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
)
308 void wxScrollBar::UpdateThumb()
312 for ( size_t n
= 0; n
< WXSIZEOF(m_elementsState
); n
++ )
314 if ( m_elementsState
[n
] & wxCONTROL_DIRTY
)
316 wxRect rect
= GetRenderer()->GetScrollbarRect(this, (Element
)n
);
318 if ( rect
.width
&& rect
.height
)
320 // we try to avoid redrawing the entire shaft (which might
321 // be quite long) if possible by only redrawing the area
322 // wich really changed
323 if ( (n
== Element_Bar_1
|| n
== Element_Bar_2
) &&
324 (m_thumbPosOld
!= -1) )
326 // the less efficient but more reliable (i.e. this will
327 // probably work everywhere) version: refresh the
328 // distance covered by thumb since the last update
331 GetRenderer()->GetScrollbarRect(this,
336 if ( n
== Element_Bar_1
)
337 rect
.SetTop(rectOld
.GetBottom());
339 rect
.SetBottom(rectOld
.GetBottom());
343 if ( n
== Element_Bar_1
)
344 rect
.SetLeft(rectOld
.GetRight());
346 rect
.SetRight(rectOld
.GetRight());
348 #else // efficient version: only repaint the area occupied by
349 // the thumb previously - we can't do better than this
350 rect
= GetRenderer()->GetScrollbarRect(this,
356 #ifdef WXDEBUG_SCROLLBAR
357 static bool s_refreshDebug
= FALSE
;
358 if ( s_refreshDebug
)
361 dc
.SetBrush(*wxCYAN_BRUSH
);
362 dc
.SetPen(*wxTRANSPARENT_PEN
);
363 dc
.DrawRectangle(rect
);
365 // under Unix we use "--sync" X option for this
366 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
371 #endif // WXDEBUG_SCROLLBAR
373 Refresh(TRUE
, &rect
);
376 m_elementsState
[n
] &= ~wxCONTROL_DIRTY
;
384 void wxScrollBar::DoDraw(wxControlRenderer
*renderer
)
386 renderer
->DrawScrollbar(this, m_thumbPosOld
);
388 // clear all dirty flags
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 static inline wxScrollBar::Element
ElementForArrow(wxScrollArrows::Arrow arrow
)
399 return arrow
== wxScrollArrows::Arrow_First
400 ? wxScrollBar::Element_Arrow_Line_1
401 : wxScrollBar::Element_Arrow_Line_2
;
404 int wxScrollBar::GetArrowState(wxScrollArrows::Arrow arrow
) const
406 return GetState(ElementForArrow(arrow
));
409 void wxScrollBar::SetArrowFlag(wxScrollArrows::Arrow arrow
, int flag
, bool set
)
411 Element which
= ElementForArrow(arrow
);
412 int state
= GetState(which
);
418 SetState(which
, state
);
421 int wxScrollBar::GetState(Element which
) const
423 // if the entire scrollbar is disabled, all of its elements are too
424 int flags
= m_elementsState
[which
];
426 flags
|= wxCONTROL_DISABLED
;
431 void wxScrollBar::SetState(Element which
, int flags
)
433 if ( (int)(m_elementsState
[which
] & ~wxCONTROL_DIRTY
) != flags
)
435 m_elementsState
[which
] = flags
| wxCONTROL_DIRTY
;
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 bool wxScrollBar::OnArrow(wxScrollArrows::Arrow arrow
)
447 int oldThumbPos
= GetThumbPosition();
448 PerformAction(arrow
== wxScrollArrows::Arrow_First
449 ? wxACTION_SCROLL_LINE_UP
450 : wxACTION_SCROLL_LINE_DOWN
);
452 // did we scroll till the end?
453 return GetThumbPosition() != oldThumbPos
;
456 bool wxScrollBar::PerformAction(const wxControlAction
& action
,
458 const wxString
& strArg
)
460 int thumbOld
= m_thumbPos
;
462 bool notify
= FALSE
; // send an event about the change?
464 wxEventType scrollType
;
466 // test for thumb move first as these events happen in quick succession
467 if ( action
== wxACTION_SCROLL_THUMB_MOVE
)
471 // VS: we have to force redraw here, otherwise the thumb will lack
472 // behind mouse cursor
475 scrollType
= wxEVT_SCROLLWIN_THUMBTRACK
;
477 else if ( action
== wxACTION_SCROLL_LINE_UP
)
479 scrollType
= wxEVT_SCROLLWIN_LINEUP
;
482 else if ( action
== wxACTION_SCROLL_LINE_DOWN
)
484 scrollType
= wxEVT_SCROLLWIN_LINEDOWN
;
487 else if ( action
== wxACTION_SCROLL_PAGE_UP
)
489 scrollType
= wxEVT_SCROLLWIN_PAGEUP
;
492 else if ( action
== wxACTION_SCROLL_PAGE_DOWN
)
494 scrollType
= wxEVT_SCROLLWIN_PAGEDOWN
;
497 else if ( action
== wxACTION_SCROLL_START
)
499 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
; // anything better?
502 else if ( action
== wxACTION_SCROLL_END
)
504 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
; // anything better?
507 else if ( action
== wxACTION_SCROLL_THUMB_DRAG
)
509 // we won't use it but this line suppresses the compiler
510 // warning about "variable may be used without having been
512 scrollType
= wxEVT_NULL
;
514 else if ( action
== wxACTION_SCROLL_THUMB_RELEASE
)
516 // always notify about this
518 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
521 return wxControl::PerformAction(action
, numArg
, strArg
);
523 // has scrollbar position changed?
524 bool changed
= m_thumbPos
!= thumbOld
;
525 if ( notify
|| changed
)
527 wxScrollWinEvent
event(scrollType
, m_thumbPos
,
528 IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
529 event
.SetEventObject(this);
530 GetParent()->GetEventHandler()->ProcessEvent(event
);
536 void wxScrollBar::ScrollToStart()
541 void wxScrollBar::ScrollToEnd()
543 DoSetThumb(m_range
- m_thumbSize
);
546 bool wxScrollBar::ScrollLines(int nLines
)
548 DoSetThumb(m_thumbPos
+ nLines
);
552 bool wxScrollBar::ScrollPages(int nPages
)
554 DoSetThumb(m_thumbPos
+ nPages
*m_pageSize
);
558 // ============================================================================
559 // scroll bar input handler
560 // ============================================================================
562 // ----------------------------------------------------------------------------
564 // ----------------------------------------------------------------------------
566 wxScrollBarTimer::wxScrollBarTimer(wxStdScrollBarInputHandler
*handler
,
567 const wxControlAction
& action
,
568 wxScrollBar
*control
)
575 bool wxScrollBarTimer::DoNotify()
577 return m_handler
->OnScrollTimer(m_control
, m_action
);
580 // ----------------------------------------------------------------------------
581 // wxStdScrollBarInputHandler
582 // ----------------------------------------------------------------------------
584 wxStdScrollBarInputHandler::wxStdScrollBarInputHandler(wxRenderer
*renderer
,
585 wxInputHandler
*handler
)
586 : wxStdInputHandler(handler
)
588 m_renderer
= renderer
;
590 m_htLast
= wxHT_NOWHERE
;
591 m_timerScroll
= NULL
;
594 wxStdScrollBarInputHandler::~wxStdScrollBarInputHandler()
596 // normally, it's NULL by now but just in case the user somehow managed to
597 // keep the mouse captured until now...
598 delete m_timerScroll
;
601 void wxStdScrollBarInputHandler::SetElementState(wxScrollBar
*control
,
605 if ( m_htLast
> wxHT_SCROLLBAR_FIRST
&& m_htLast
< wxHT_SCROLLBAR_LAST
)
608 elem
= (wxScrollBar::Element
)(m_htLast
- wxHT_SCROLLBAR_FIRST
- 1);
610 int flags
= control
->GetState(elem
);
615 control
->SetState(elem
, flags
);
619 bool wxStdScrollBarInputHandler::OnScrollTimer(wxScrollBar
*scrollbar
,
620 const wxControlAction
& action
)
622 int oldThumbPos
= scrollbar
->GetThumbPosition();
623 scrollbar
->PerformAction(action
);
624 if ( scrollbar
->GetThumbPosition() != oldThumbPos
)
627 // we scrolled till the end
628 m_timerScroll
->Stop();
633 void wxStdScrollBarInputHandler::StopScrolling(wxScrollBar
*control
)
635 // return everything to the normal state
638 m_winCapture
->ReleaseMouse();
646 delete m_timerScroll
;
647 m_timerScroll
= NULL
;
650 // unpress the arrow and highlight the current element
651 Press(control
, FALSE
);
655 wxStdScrollBarInputHandler::GetMouseCoord(const wxScrollBar
*scrollbar
,
656 const wxMouseEvent
& event
) const
658 wxPoint pt
= event
.GetPosition();
659 return scrollbar
->GetWindowStyle() & wxVERTICAL
? pt
.y
: pt
.x
;
662 void wxStdScrollBarInputHandler::HandleThumbMove(wxScrollBar
*scrollbar
,
663 const wxMouseEvent
& event
)
665 int thumbPos
= GetMouseCoord(scrollbar
, event
) - m_ofsMouse
;
666 thumbPos
= m_renderer
->PixelToScrollbar(scrollbar
, thumbPos
);
667 scrollbar
->PerformAction(wxACTION_SCROLL_THUMB_MOVE
, thumbPos
);
670 bool wxStdScrollBarInputHandler::HandleKey(wxInputConsumer
*consumer
,
671 const wxKeyEvent
& event
,
674 // we only react to the key presses here
677 wxControlAction action
;
678 switch ( event
.GetKeyCode() )
681 case WXK_RIGHT
: action
= wxACTION_SCROLL_LINE_DOWN
; break;
683 case WXK_LEFT
: action
= wxACTION_SCROLL_LINE_UP
; break;
684 case WXK_HOME
: action
= wxACTION_SCROLL_START
; break;
685 case WXK_END
: action
= wxACTION_SCROLL_END
; break;
687 case WXK_PRIOR
: action
= wxACTION_SCROLL_PAGE_UP
; break;
689 case WXK_NEXT
: action
= wxACTION_SCROLL_PAGE_DOWN
; break;
694 consumer
->PerformAction(action
);
700 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
703 bool wxStdScrollBarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
704 const wxMouseEvent
& event
)
706 // is this a click event from an acceptable button?
707 int btn
= event
.GetButton();
708 if ( (btn
!= -1) && IsAllowedButton(btn
) )
710 // determine which part of the window mouse is in
711 wxScrollBar
*scrollbar
= wxStaticCast(consumer
->GetInputWindow(), wxScrollBar
);
712 wxHitTest ht
= m_renderer
->HitTestScrollbar
718 // when the mouse is pressed on any scrollbar element, we capture it
719 // and hold capture until the same mouse button is released
720 if ( event
.ButtonDown() || event
.ButtonDClick() )
725 m_winCapture
= consumer
->GetInputWindow();
726 m_winCapture
->CaptureMouse();
728 // generate the command
729 bool hasAction
= TRUE
;
730 wxControlAction action
;
733 case wxHT_SCROLLBAR_ARROW_LINE_1
:
734 action
= wxACTION_SCROLL_LINE_UP
;
737 case wxHT_SCROLLBAR_ARROW_LINE_2
:
738 action
= wxACTION_SCROLL_LINE_DOWN
;
741 case wxHT_SCROLLBAR_BAR_1
:
742 action
= wxACTION_SCROLL_PAGE_UP
;
743 m_ptStartScrolling
= event
.GetPosition();
746 case wxHT_SCROLLBAR_BAR_2
:
747 action
= wxACTION_SCROLL_PAGE_DOWN
;
748 m_ptStartScrolling
= event
.GetPosition();
751 case wxHT_SCROLLBAR_THUMB
:
752 consumer
->PerformAction(wxACTION_SCROLL_THUMB_DRAG
);
753 m_ofsMouse
= GetMouseCoord(scrollbar
, event
) -
754 m_renderer
->ScrollbarToPixel(scrollbar
);
756 // fall through: there is no immediate action
762 // remove highlighting
763 Highlight(scrollbar
, FALSE
);
766 // and press the arrow or highlight thumb now instead
767 if ( m_htLast
== wxHT_SCROLLBAR_THUMB
)
768 Highlight(scrollbar
, TRUE
);
770 Press(scrollbar
, TRUE
);
775 m_timerScroll
= new wxScrollBarTimer(this, action
,
777 m_timerScroll
->StartAutoScroll();
779 //else: no (immediate) action
782 //else: mouse already captured, nothing to do
784 // release mouse if the *same* button went up
785 else if ( btn
== m_btnCapture
)
789 StopScrolling(scrollbar
);
791 // if we were dragging the thumb, send the last event
792 if ( m_htLast
== wxHT_SCROLLBAR_THUMB
)
794 scrollbar
->PerformAction(wxACTION_SCROLL_THUMB_RELEASE
);
798 Highlight(scrollbar
, TRUE
);
802 // this is not supposed to happen as the button can't go up
803 // without going down previously and then we'd have
804 // m_winCapture by now
805 wxFAIL_MSG( _T("logic error in mouse capturing code") );
810 return wxStdInputHandler::HandleMouse(consumer
, event
);
813 bool wxStdScrollBarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
814 const wxMouseEvent
& event
)
816 wxScrollBar
*scrollbar
= wxStaticCast(consumer
->GetInputWindow(), wxScrollBar
);
820 if ( (m_htLast
== wxHT_SCROLLBAR_THUMB
) && event
.Moving() )
822 // make the thumb follow the mouse by keeping the same offset
823 // between the mouse position and the top/left of the thumb
824 HandleThumbMove(scrollbar
, event
);
829 // no other changes are possible while the mouse is captured
833 bool isArrow
= scrollbar
->GetArrows().HandleMouseMove(event
);
835 if ( event
.Moving() )
837 wxHitTest ht
= m_renderer
->HitTestScrollbar
842 if ( ht
== m_htLast
)
849 wxLogDebug("Scrollbar::OnMouseMove: ht = %d", ht
);
850 #endif // DEBUG_MOUSE
852 Highlight(scrollbar
, FALSE
);
856 Highlight(scrollbar
, TRUE
);
857 //else: already done by wxScrollArrows::HandleMouseMove
859 else if ( event
.Leaving() )
862 Highlight(scrollbar
, FALSE
);
864 m_htLast
= wxHT_NOWHERE
;
866 else // event.Entering()
868 // we don't process this event
876 #endif // wxUSE_SCROLLBAR
878 // ----------------------------------------------------------------------------
880 // ----------------------------------------------------------------------------
882 wxScrollTimer::wxScrollTimer()
887 void wxScrollTimer::StartAutoScroll()
889 // start scrolling immediately
892 // ... and end it too
896 // there is an initial delay before the scrollbar starts scrolling -
897 // implement it by ignoring the first timer expiration and only start
898 // scrolling from the second one
900 Start(200); // FIXME: hardcoded delay
903 void wxScrollTimer::Notify()
907 // scroll normally now - reduce the delay
909 Start(50); // FIXME: hardcoded delay
915 // if DoNotify() returns false, we're already deleted by the timer
916 // event handler, so don't do anything else here