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 // ----------------------------------------------------------------------------
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"
47 #define WXDEBUG_SCROLLBAR
50 #undef WXDEBUG_SCROLLBAR
51 #endif // !__WXDEBUG__
53 #if defined(WXDEBUG_SCROLLBAR) && defined(__WXMSW__) && !defined(__WXMICROWIN__)
54 #include "wx/msw/private.h"
57 // ----------------------------------------------------------------------------
58 // wxScrollBarTimer: this class is used to repeatedly scroll the scrollbar
59 // when the mouse is help pressed on the arrow or on the bar. It generates the
60 // given scroll action command periodically.
61 // ----------------------------------------------------------------------------
63 class wxScrollBarTimer
: public wxScrollTimer
66 wxScrollBarTimer(wxStdScrollBarInputHandler
*handler
,
67 const wxControlAction
& action
,
68 wxScrollBar
*control
);
71 virtual bool DoNotify();
74 wxStdScrollBarInputHandler
*m_handler
;
75 wxControlAction m_action
;
76 wxScrollBar
*m_control
;
79 // ============================================================================
81 // ============================================================================
83 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
, wxControl
)
85 BEGIN_EVENT_TABLE(wxScrollBar
, wxScrollBarBase
)
86 EVT_IDLE(wxScrollBar::OnIdle
)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
94 // warning C4355: 'this' : used in base member initializer list
95 #pragma warning(disable:4355) // so what? disable it...
98 wxScrollBar::wxScrollBar()
104 wxScrollBar::wxScrollBar(wxWindow
*parent
,
109 const wxValidator
& validator
,
110 const wxString
& name
)
115 (void)Create(parent
, id
, pos
, size
, style
, validator
, name
);
119 // warning C4355: 'this' : used in base member initializer list
120 #pragma warning(default:4355)
123 void wxScrollBar::Init()
132 for ( size_t n
= 0; n
< WXSIZEOF(m_elementsState
); n
++ )
134 m_elementsState
[n
] = 0;
140 bool wxScrollBar::Create(wxWindow
*parent
,
145 const wxValidator
& validator
,
146 const wxString
&name
)
148 // the scrollbars never have the border
149 style
&= ~wxBORDER_MASK
;
151 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
156 // override the cursor of the target window (if any)
157 SetCursor(wxCURSOR_ARROW
);
159 CreateInputHandler(wxINP_HANDLER_SCROLLBAR
);
164 wxScrollBar::~wxScrollBar()
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 bool wxScrollBar::IsStandalone() const
174 wxWindow
*parent
= GetParent();
180 return (parent
->GetScrollbar(wxHORIZONTAL
) != this) &&
181 (parent
->GetScrollbar(wxVERTICAL
) != this);
184 bool wxScrollBar::AcceptsFocus() const
186 // the window scrollbars never accept focus
187 return wxScrollBarBase::AcceptsFocus() && IsStandalone();
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 void wxScrollBar::DoSetThumb(int pos
)
196 // don't assert hecks here, we're a private function which is meant to be
197 // called with any args at all
202 else if ( pos
> m_range
- m_thumbSize
)
204 pos
= m_range
- m_thumbSize
;
207 if ( m_thumbPos
== pos
)
209 // nothing changed, avoid refreshes which would provoke flicker
213 if ( m_thumbPosOld
== -1 )
215 // remember the old thumb position
216 m_thumbPosOld
= m_thumbPos
;
221 // we have to refresh the part of the bar which was under the thumb and the
223 m_elementsState
[Element_Thumb
] |= wxCONTROL_DIRTY
;
224 m_elementsState
[m_thumbPos
> m_thumbPosOld
225 ? Element_Bar_1
: Element_Bar_2
] |= wxCONTROL_DIRTY
;
229 int wxScrollBar::GetThumbPosition() const
234 int wxScrollBar::GetThumbSize() const
239 int wxScrollBar::GetPageSize() const
244 int wxScrollBar::GetRange() const
249 void wxScrollBar::SetThumbPosition(int pos
)
251 wxCHECK_RET( pos
>= 0 && pos
<= m_range
, _T("thumb position out of range") );
256 void wxScrollBar::SetScrollbar(int position
, int thumbSize
,
257 int range
, int pageSize
,
260 // we only refresh everythign when the range changes, thumb position
261 // changes are handled in OnIdle
262 bool needsRefresh
= (range
!= m_range
) ||
263 (thumbSize
!= m_thumbSize
) ||
264 (pageSize
!= m_pageSize
);
266 // set all parameters
268 m_thumbSize
= thumbSize
;
269 SetThumbPosition(position
);
270 m_pageSize
= pageSize
;
272 // ignore refresh parameter unless we really need to refresh everything -
273 // there ir a lot of existing code which just calls SetScrollbar() without
274 // specifying the last parameter even though it doesn't need at all to
275 // refresh the window immediately
276 if ( refresh
&& needsRefresh
)
278 // and update the window
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 wxSize
wxScrollBar::DoGetBestClientSize() const
290 // this dimension is completely arbitrary
291 static const wxCoord SIZE
= 140;
293 wxSize size
= m_renderer
->GetScrollbarArrowSize();
306 wxScrollArrows::Arrow
wxScrollBar::HitTest(const wxPoint
& pt
) const
308 switch ( m_renderer
->HitTestScrollbar(this, pt
) )
310 case wxHT_SCROLLBAR_ARROW_LINE_1
:
311 return wxScrollArrows::Arrow_First
;
313 case wxHT_SCROLLBAR_ARROW_LINE_2
:
314 return wxScrollArrows::Arrow_Second
;
317 return wxScrollArrows::Arrow_None
;
321 // ----------------------------------------------------------------------------
323 // ----------------------------------------------------------------------------
325 void wxScrollBar::OnIdle(wxIdleEvent
& event
)
331 void wxScrollBar::UpdateThumb()
335 for ( size_t n
= 0; n
< WXSIZEOF(m_elementsState
); n
++ )
337 if ( m_elementsState
[n
] & wxCONTROL_DIRTY
)
339 wxRect rect
= GetRenderer()->GetScrollbarRect(this, (Element
)n
);
341 if ( rect
.width
&& rect
.height
)
343 // we try to avoid redrawing the entire shaft (which might
344 // be quite long) if possible by only redrawing the area
345 // wich really changed
346 if ( (n
== Element_Bar_1
|| n
== Element_Bar_2
) &&
347 (m_thumbPosOld
!= -1) )
349 // the less efficient but more reliable (i.e. this will
350 // probably work everywhere) version: refresh the
351 // distance covered by thumb since the last update
354 GetRenderer()->GetScrollbarRect(this,
359 if ( n
== Element_Bar_1
)
360 rect
.SetTop(rectOld
.GetBottom());
362 rect
.SetBottom(rectOld
.GetBottom());
366 if ( n
== Element_Bar_1
)
367 rect
.SetLeft(rectOld
.GetRight());
369 rect
.SetRight(rectOld
.GetRight());
371 #else // efficient version: only repaint the area occupied by
372 // the thumb previously - we can't do better than this
373 rect
= GetRenderer()->GetScrollbarRect(this,
379 #ifdef WXDEBUG_SCROLLBAR
380 static bool s_refreshDebug
= FALSE
;
381 if ( s_refreshDebug
)
384 dc
.SetBrush(*wxCYAN_BRUSH
);
385 dc
.SetPen(*wxTRANSPARENT_PEN
);
386 dc
.DrawRectangle(rect
);
388 // under Unix we use "--sync" X option for this
389 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
394 #endif // WXDEBUG_SCROLLBAR
396 Refresh(FALSE
, &rect
);
399 m_elementsState
[n
] &= ~wxCONTROL_DIRTY
;
407 void wxScrollBar::DoDraw(wxControlRenderer
*renderer
)
409 renderer
->DrawScrollbar(this, m_thumbPosOld
);
411 // clear all dirty flags
416 // ----------------------------------------------------------------------------
418 // ----------------------------------------------------------------------------
420 static inline wxScrollBar::Element
ElementForArrow(wxScrollArrows::Arrow arrow
)
422 return arrow
== wxScrollArrows::Arrow_First
423 ? wxScrollBar::Element_Arrow_Line_1
424 : wxScrollBar::Element_Arrow_Line_2
;
427 int wxScrollBar::GetArrowState(wxScrollArrows::Arrow arrow
) const
429 return GetState(ElementForArrow(arrow
));
432 void wxScrollBar::SetArrowFlag(wxScrollArrows::Arrow arrow
, int flag
, bool set
)
434 Element which
= ElementForArrow(arrow
);
435 int state
= GetState(which
);
441 SetState(which
, state
);
444 int wxScrollBar::GetState(Element which
) const
446 // if the entire scrollbar is disabled, all of its elements are too
447 int flags
= m_elementsState
[which
];
449 flags
|= wxCONTROL_DISABLED
;
454 void wxScrollBar::SetState(Element which
, int flags
)
456 if ( (int)(m_elementsState
[which
] & ~wxCONTROL_DIRTY
) != flags
)
458 m_elementsState
[which
] = flags
| wxCONTROL_DIRTY
;
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
468 bool wxScrollBar::OnArrow(wxScrollArrows::Arrow arrow
)
470 int oldThumbPos
= GetThumbPosition();
471 PerformAction(arrow
== wxScrollArrows::Arrow_First
472 ? wxACTION_SCROLL_LINE_UP
473 : wxACTION_SCROLL_LINE_DOWN
);
475 // did we scroll till the end?
476 return GetThumbPosition() != oldThumbPos
;
479 bool wxScrollBar::PerformAction(const wxControlAction
& action
,
481 const wxString
& strArg
)
483 int thumbOld
= m_thumbPos
;
485 bool notify
= FALSE
; // send an event about the change?
487 wxEventType scrollType
;
489 // test for thumb move first as these events happen in quick succession
490 if ( action
== wxACTION_SCROLL_THUMB_MOVE
)
494 // VS: we have to force redraw here, otherwise the thumb will lack
495 // behind mouse cursor
498 scrollType
= wxEVT_SCROLLWIN_THUMBTRACK
;
500 else if ( action
== wxACTION_SCROLL_LINE_UP
)
502 scrollType
= wxEVT_SCROLLWIN_LINEUP
;
505 else if ( action
== wxACTION_SCROLL_LINE_DOWN
)
507 scrollType
= wxEVT_SCROLLWIN_LINEDOWN
;
510 else if ( action
== wxACTION_SCROLL_PAGE_UP
)
512 scrollType
= wxEVT_SCROLLWIN_PAGEUP
;
515 else if ( action
== wxACTION_SCROLL_PAGE_DOWN
)
517 scrollType
= wxEVT_SCROLLWIN_PAGEDOWN
;
520 else if ( action
== wxACTION_SCROLL_START
)
522 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
; // anything better?
525 else if ( action
== wxACTION_SCROLL_END
)
527 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
; // anything better?
530 else if ( action
== wxACTION_SCROLL_THUMB_DRAG
)
532 // we won't use it but this line suppresses the compiler
533 // warning about "variable may be used without having been
535 scrollType
= wxEVT_NULL
;
537 else if ( action
== wxACTION_SCROLL_THUMB_RELEASE
)
539 // always notify about this
541 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
544 return wxControl::PerformAction(action
, numArg
, strArg
);
546 // has scrollbar position changed?
547 bool changed
= m_thumbPos
!= thumbOld
;
548 if ( notify
|| changed
)
550 if ( IsStandalone() )
552 // we should generate EVT_SCROLL events for the standalone
553 // scrollbars and not the EVT_SCROLLWIN ones
555 // NB: we assume that scrollbar events are sequentially numbered
556 // but this should be ok as other code relies on this as well
557 scrollType
+= wxEVT_SCROLL_TOP
- wxEVT_SCROLLWIN_TOP
;
560 wxScrollWinEvent
event(scrollType
, m_thumbPos
,
561 IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
562 event
.SetEventObject(this);
563 GetParent()->GetEventHandler()->ProcessEvent(event
);
569 void wxScrollBar::ScrollToStart()
574 void wxScrollBar::ScrollToEnd()
576 DoSetThumb(m_range
- m_thumbSize
);
579 bool wxScrollBar::ScrollLines(int nLines
)
581 DoSetThumb(m_thumbPos
+ nLines
);
585 bool wxScrollBar::ScrollPages(int nPages
)
587 DoSetThumb(m_thumbPos
+ nPages
*m_pageSize
);
591 // ============================================================================
592 // scroll bar input handler
593 // ============================================================================
595 // ----------------------------------------------------------------------------
597 // ----------------------------------------------------------------------------
599 wxScrollBarTimer::wxScrollBarTimer(wxStdScrollBarInputHandler
*handler
,
600 const wxControlAction
& action
,
601 wxScrollBar
*control
)
608 bool wxScrollBarTimer::DoNotify()
610 return m_handler
->OnScrollTimer(m_control
, m_action
);
613 // ----------------------------------------------------------------------------
614 // wxStdScrollBarInputHandler
615 // ----------------------------------------------------------------------------
617 wxStdScrollBarInputHandler::wxStdScrollBarInputHandler(wxRenderer
*renderer
,
618 wxInputHandler
*handler
)
619 : wxStdInputHandler(handler
)
621 m_renderer
= renderer
;
623 m_htLast
= wxHT_NOWHERE
;
624 m_timerScroll
= NULL
;
627 wxStdScrollBarInputHandler::~wxStdScrollBarInputHandler()
629 // normally, it's NULL by now but just in case the user somehow managed to
630 // keep the mouse captured until now...
631 delete m_timerScroll
;
634 void wxStdScrollBarInputHandler::SetElementState(wxScrollBar
*control
,
638 if ( m_htLast
> wxHT_SCROLLBAR_FIRST
&& m_htLast
< wxHT_SCROLLBAR_LAST
)
641 elem
= (wxScrollBar::Element
)(m_htLast
- wxHT_SCROLLBAR_FIRST
- 1);
643 int flags
= control
->GetState(elem
);
648 control
->SetState(elem
, flags
);
652 bool wxStdScrollBarInputHandler::OnScrollTimer(wxScrollBar
*scrollbar
,
653 const wxControlAction
& action
)
655 int oldThumbPos
= scrollbar
->GetThumbPosition();
656 scrollbar
->PerformAction(action
);
657 if ( scrollbar
->GetThumbPosition() != oldThumbPos
)
660 // we scrolled till the end
661 m_timerScroll
->Stop();
666 void wxStdScrollBarInputHandler::StopScrolling(wxScrollBar
*control
)
668 // return everything to the normal state
671 m_winCapture
->ReleaseMouse();
679 delete m_timerScroll
;
680 m_timerScroll
= NULL
;
683 // unpress the arrow and highlight the current element
684 Press(control
, FALSE
);
688 wxStdScrollBarInputHandler::GetMouseCoord(const wxScrollBar
*scrollbar
,
689 const wxMouseEvent
& event
) const
691 wxPoint pt
= event
.GetPosition();
692 return scrollbar
->GetWindowStyle() & wxVERTICAL
? pt
.y
: pt
.x
;
695 void wxStdScrollBarInputHandler::HandleThumbMove(wxScrollBar
*scrollbar
,
696 const wxMouseEvent
& event
)
698 int thumbPos
= GetMouseCoord(scrollbar
, event
) - m_ofsMouse
;
699 thumbPos
= m_renderer
->PixelToScrollbar(scrollbar
, thumbPos
);
700 scrollbar
->PerformAction(wxACTION_SCROLL_THUMB_MOVE
, thumbPos
);
703 bool wxStdScrollBarInputHandler::HandleKey(wxInputConsumer
*consumer
,
704 const wxKeyEvent
& event
,
707 // we only react to the key presses here
710 wxControlAction action
;
711 switch ( event
.GetKeyCode() )
714 case WXK_RIGHT
: action
= wxACTION_SCROLL_LINE_DOWN
; break;
716 case WXK_LEFT
: action
= wxACTION_SCROLL_LINE_UP
; break;
717 case WXK_HOME
: action
= wxACTION_SCROLL_START
; break;
718 case WXK_END
: action
= wxACTION_SCROLL_END
; break;
720 case WXK_PRIOR
: action
= wxACTION_SCROLL_PAGE_UP
; break;
722 case WXK_NEXT
: action
= wxACTION_SCROLL_PAGE_DOWN
; break;
727 consumer
->PerformAction(action
);
733 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
736 bool wxStdScrollBarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
737 const wxMouseEvent
& event
)
739 // is this a click event from an acceptable button?
740 int btn
= event
.GetButton();
741 if ( (btn
!= -1) && IsAllowedButton(btn
) )
743 // determine which part of the window mouse is in
744 wxScrollBar
*scrollbar
= wxStaticCast(consumer
->GetInputWindow(), wxScrollBar
);
745 wxHitTest ht
= m_renderer
->HitTestScrollbar
751 // when the mouse is pressed on any scrollbar element, we capture it
752 // and hold capture until the same mouse button is released
753 if ( event
.ButtonDown() || event
.ButtonDClick() )
758 m_winCapture
= consumer
->GetInputWindow();
759 m_winCapture
->CaptureMouse();
761 // generate the command
762 bool hasAction
= TRUE
;
763 wxControlAction action
;
766 case wxHT_SCROLLBAR_ARROW_LINE_1
:
767 action
= wxACTION_SCROLL_LINE_UP
;
770 case wxHT_SCROLLBAR_ARROW_LINE_2
:
771 action
= wxACTION_SCROLL_LINE_DOWN
;
774 case wxHT_SCROLLBAR_BAR_1
:
775 action
= wxACTION_SCROLL_PAGE_UP
;
776 m_ptStartScrolling
= event
.GetPosition();
779 case wxHT_SCROLLBAR_BAR_2
:
780 action
= wxACTION_SCROLL_PAGE_DOWN
;
781 m_ptStartScrolling
= event
.GetPosition();
784 case wxHT_SCROLLBAR_THUMB
:
785 consumer
->PerformAction(wxACTION_SCROLL_THUMB_DRAG
);
786 m_ofsMouse
= GetMouseCoord(scrollbar
, event
) -
787 m_renderer
->ScrollbarToPixel(scrollbar
);
789 // fall through: there is no immediate action
795 // remove highlighting
796 Highlight(scrollbar
, FALSE
);
799 // and press the arrow or highlight thumb now instead
800 if ( m_htLast
== wxHT_SCROLLBAR_THUMB
)
801 Highlight(scrollbar
, TRUE
);
803 Press(scrollbar
, TRUE
);
808 m_timerScroll
= new wxScrollBarTimer(this, action
,
810 m_timerScroll
->StartAutoScroll();
812 //else: no (immediate) action
815 //else: mouse already captured, nothing to do
817 // release mouse if the *same* button went up
818 else if ( btn
== m_btnCapture
)
822 StopScrolling(scrollbar
);
824 // if we were dragging the thumb, send the last event
825 if ( m_htLast
== wxHT_SCROLLBAR_THUMB
)
827 scrollbar
->PerformAction(wxACTION_SCROLL_THUMB_RELEASE
);
831 Highlight(scrollbar
, TRUE
);
835 // this is not supposed to happen as the button can't go up
836 // without going down previously and then we'd have
837 // m_winCapture by now
838 wxFAIL_MSG( _T("logic error in mouse capturing code") );
843 return wxStdInputHandler::HandleMouse(consumer
, event
);
846 bool wxStdScrollBarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
847 const wxMouseEvent
& event
)
849 wxScrollBar
*scrollbar
= wxStaticCast(consumer
->GetInputWindow(), wxScrollBar
);
853 if ( (m_htLast
== wxHT_SCROLLBAR_THUMB
) && event
.Moving() )
855 // make the thumb follow the mouse by keeping the same offset
856 // between the mouse position and the top/left of the thumb
857 HandleThumbMove(scrollbar
, event
);
862 // no other changes are possible while the mouse is captured
866 bool isArrow
= scrollbar
->GetArrows().HandleMouseMove(event
);
868 if ( event
.Moving() )
870 wxHitTest ht
= m_renderer
->HitTestScrollbar
875 if ( ht
== m_htLast
)
882 wxLogDebug("Scrollbar::OnMouseMove: ht = %d", ht
);
883 #endif // DEBUG_MOUSE
885 Highlight(scrollbar
, FALSE
);
889 Highlight(scrollbar
, TRUE
);
890 //else: already done by wxScrollArrows::HandleMouseMove
892 else if ( event
.Leaving() )
895 Highlight(scrollbar
, FALSE
);
897 m_htLast
= wxHT_NOWHERE
;
899 else // event.Entering()
901 // we don't process this event
909 #endif // wxUSE_SCROLLBAR
911 // ----------------------------------------------------------------------------
913 // ----------------------------------------------------------------------------
915 wxScrollTimer::wxScrollTimer()
920 void wxScrollTimer::StartAutoScroll()
922 // start scrolling immediately
925 // ... and end it too
929 // there is an initial delay before the scrollbar starts scrolling -
930 // implement it by ignoring the first timer expiration and only start
931 // scrolling from the second one
933 Start(200); // FIXME: hardcoded delay
936 void wxScrollTimer::Notify()
940 // scroll normally now - reduce the delay
942 Start(50); // FIXME: hardcoded delay
948 // if DoNotify() returns false, we're already deleted by the timer
949 // event handler, so don't do anything else here