1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/scrlwing.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin on 31.08.00: wxScrollHelper allows to implement.
6 // Ron Lee on 10.4.02: virtual size / auto scrollbars et al.
9 // Copyright: (c) wxWidgets team
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
28 #include "wx/scrolwin.h"
33 #include "wx/dcclient.h"
36 #include "wx/settings.h"
40 #include "wx/scrolbar.h"
43 #include "wx/recguard.h"
46 #include <windows.h> // for DLGC_WANTARROWS
47 #include "wx/msw/winundef.h"
51 // For wxRETAINED implementation
52 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
53 //This code switches off the compiler warnings
54 # pragma message disable nosimpint
58 # pragma message enable nosimpint
64 style wxHSCROLL | wxVSCROLL
67 // ----------------------------------------------------------------------------
68 // wxScrollHelperEvtHandler: intercept the events from the window and forward
69 // them to wxScrollHelper
70 // ----------------------------------------------------------------------------
72 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
75 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
77 m_scrollHelper
= scrollHelper
;
80 virtual bool ProcessEvent(wxEvent
& event
);
82 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
85 wxScrollHelper
*m_scrollHelper
;
87 bool m_hasDrawnWindow
;
89 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
93 // ----------------------------------------------------------------------------
94 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
95 // a captured mouse is held outside the window
96 // ----------------------------------------------------------------------------
98 class wxAutoScrollTimer
: public wxTimer
101 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
102 wxEventType eventTypeToSend
,
103 int pos
, int orient
);
105 virtual void Notify();
109 wxScrollHelper
*m_scrollHelper
;
110 wxEventType m_eventType
;
114 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 wxAutoScrollTimer
::wxAutoScrollTimer(wxWindow
*winToScroll
,
126 wxScrollHelper
*scroll
,
127 wxEventType eventTypeToSend
,
131 m_scrollHelper
= scroll
;
132 m_eventType
= eventTypeToSend
;
137 void wxAutoScrollTimer
::Notify()
139 // only do all this as long as the window is capturing the mouse
140 if ( wxWindow
::GetCapture() != m_win
)
144 else // we still capture the mouse, continue generating events
146 // first scroll the window if we are allowed to do it
147 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
148 event1
.SetEventObject(m_win
);
149 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
150 m_win
->GetEventHandler()->ProcessEvent(event1
) )
152 // and then send a pseudo mouse-move event to refresh the selection
153 wxMouseEvent
event2(wxEVT_MOTION
);
154 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
156 // the mouse event coordinates should be client, not screen as
157 // returned by wxGetMousePosition
158 wxWindow
*parentTop
= m_win
;
159 while ( parentTop
->GetParent() )
160 parentTop
= parentTop
->GetParent();
161 wxPoint ptOrig
= parentTop
->GetPosition();
162 event2
.m_x
-= ptOrig
.x
;
163 event2
.m_y
-= ptOrig
.y
;
165 event2
.SetEventObject(m_win
);
167 // FIXME: we don't fill in the other members - ok?
169 m_win
->GetEventHandler()->ProcessEvent(event2
);
171 else // can't scroll further, stop
179 // ----------------------------------------------------------------------------
180 // wxScrollHelperEvtHandler
181 // ----------------------------------------------------------------------------
183 bool wxScrollHelperEvtHandler
::ProcessEvent(wxEvent
& event
)
185 wxEventType evType
= event
.GetEventType();
187 // the explanation of wxEVT_PAINT processing hack: for historic reasons
188 // there are 2 ways to process this event in classes deriving from
189 // wxScrolledWindow. The user code may
191 // 1. override wxScrolledWindow::OnDraw(dc)
192 // 2. define its own OnPaint() handler
194 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
195 // always processes the draw event, so we can't just try the window
196 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
197 // (the latter would never be called in wxUniversal).
199 // So the solution is to have a flag telling us whether the user code drew
200 // anything in the window. We set it to true here but reset it to false in
201 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
202 // user code defined OnPaint() in the derived class)
203 m_hasDrawnWindow
= true;
205 // pass it on to the real handler
206 bool processed
= wxEvtHandler
::ProcessEvent(event
);
208 // always process the size events ourselves, even if the user code handles
209 // them as well, as we need to AdjustScrollbars()
211 // NB: it is important to do it after processing the event in the normal
212 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
213 // scrollbar[s] (dis)appear and it should be seen by the user code
215 if ( evType
== wxEVT_SIZE
)
217 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
224 // normally, nothing more to do here - except if it was a paint event
225 // which wasn't really processed, then we'll try to call our
226 // OnDraw() below (from HandleOnPaint)
227 if ( m_hasDrawnWindow
|| event
.IsCommandEvent() )
233 if ( evType
== wxEVT_PAINT
)
235 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
239 if ( evType
== wxEVT_CHILD_FOCUS
)
241 m_scrollHelper
->HandleOnChildFocus((wxChildFocusEvent
&)event
);
245 // reset the skipped flag (which might have been set to true in
246 // ProcessEvent() above) to be able to test it below
247 bool wasSkipped
= event
.GetSkipped();
251 if ( evType
== wxEVT_SCROLLWIN_TOP
||
252 evType
== wxEVT_SCROLLWIN_BOTTOM
||
253 evType
== wxEVT_SCROLLWIN_LINEUP
||
254 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
255 evType
== wxEVT_SCROLLWIN_PAGEUP
||
256 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
257 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
258 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
260 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
261 if ( !event
.GetSkipped() )
263 // it makes sense to indicate that we processed the message as we
264 // did scroll the window (and also notice that wxAutoScrollTimer
265 // relies on our return value to stop scrolling when we are at top
266 // or bottom already)
272 if ( evType
== wxEVT_ENTER_WINDOW
)
274 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
276 else if ( evType
== wxEVT_LEAVE_WINDOW
)
278 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
281 // Use GTK's own scroll wheel handling in GtkScrolledWindow
283 else if ( evType
== wxEVT_MOUSEWHEEL
)
285 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
289 #endif // wxUSE_MOUSEWHEEL
290 else if ( evType
== wxEVT_CHAR
)
292 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
293 if ( !event
.GetSkipped() )
301 event
.Skip(wasSkipped
);
306 // ----------------------------------------------------------------------------
307 // wxScrollHelper construction
308 // ----------------------------------------------------------------------------
310 wxScrollHelper
::wxScrollHelper(wxWindow
*win
)
312 wxASSERT_MSG( win
, _T("associated window can't be NULL in wxScrollHelper") );
314 m_xScrollPixelsPerLine
=
315 m_yScrollPixelsPerLine
=
320 m_xScrollLinesPerPage
=
321 m_yScrollLinesPerPage
= 0;
323 m_xScrollingEnabled
=
324 m_yScrollingEnabled
= true;
327 m_yVisibility
= wxSHOW_SB_DEFAULT
;
336 m_targetWindow
= (wxWindow
*)NULL
;
338 m_timerAutoScroll
= (wxTimer
*)NULL
;
344 m_win
->SetScrollHelper( this );
346 // by default, the associated window is also the target window
347 DoSetTargetWindow(win
);
350 wxScrollHelper
::~wxScrollHelper()
357 // ----------------------------------------------------------------------------
358 // setting scrolling parameters
359 // ----------------------------------------------------------------------------
361 void wxScrollHelper
::SetScrollbars(int pixelsPerUnitX
,
371 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
374 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
375 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
377 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
378 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
379 (xPos
!= m_xScrollPosition
) ||
380 (yPos
!= m_yScrollPosition
)
383 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
384 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
385 m_xScrollPosition
= xPos
;
386 m_yScrollPosition
= yPos
;
388 int w
= noUnitsX
* pixelsPerUnitX
;
389 int h
= noUnitsY
* pixelsPerUnitY
;
391 // For better backward compatibility we set persisting limits
392 // here not just the size. It makes SetScrollbars 'sticky'
393 // emulating the old non-autoscroll behaviour.
394 // m_targetWindow->SetVirtualSizeHints( w, h );
396 // The above should arguably be deprecated, this however we still need.
398 // take care not to set 0 virtual size, 0 means that we don't have any
399 // scrollbars and hence we should use the real size instead of the virtual
400 // one which is indicated by using wxDefaultCoord
401 m_targetWindow
->SetVirtualSize( w ? w
: wxDefaultCoord
,
402 h ? h
: wxDefaultCoord
);
404 if (do_refresh
&& !noRefresh
)
405 m_targetWindow
->Refresh(true, GetScrollRect());
407 #ifndef __WXUNIVERSAL__
408 // If the target is not the same as the window with the scrollbars,
409 // then we need to update the scrollbars here, since they won't have
410 // been updated by SetVirtualSize().
411 if ( m_targetWindow
!= m_win
)
412 #endif // !__WXUNIVERSAL__
416 #ifndef __WXUNIVERSAL__
419 // otherwise this has been done by AdjustScrollbars, above
421 #endif // !__WXUNIVERSAL__
424 // ----------------------------------------------------------------------------
425 // [target] window handling
426 // ----------------------------------------------------------------------------
428 void wxScrollHelper
::DeleteEvtHandler()
430 // search for m_handler in the handler list
431 if ( m_win
&& m_handler
)
433 if ( m_win
->RemoveEventHandler(m_handler
) )
437 //else: something is very wrong, so better [maybe] leak memory than
438 // risk a crash because of double deletion
444 void wxScrollHelper
::ResetDrawnFlag()
446 wxCHECK_RET( m_handler
, "invalid use of ResetDrawnFlag - no handler?" );
447 m_handler
->ResetDrawnFlag();
450 void wxScrollHelper
::DoSetTargetWindow(wxWindow
*target
)
452 m_targetWindow
= target
;
454 target
->MacSetClipChildren( true ) ;
457 // install the event handler which will intercept the events we're
458 // interested in (but only do it for our real window, not the target window
459 // which we scroll - we don't need to hijack its events)
460 if ( m_targetWindow
== m_win
)
462 // if we already have a handler, delete it first
465 m_handler
= new wxScrollHelperEvtHandler(this);
466 m_targetWindow
->PushEventHandler(m_handler
);
470 void wxScrollHelper
::SetTargetWindow(wxWindow
*target
)
472 wxCHECK_RET( target
, wxT("target window must not be NULL") );
474 if ( target
== m_targetWindow
)
477 DoSetTargetWindow(target
);
480 wxWindow
*wxScrollHelper
::GetTargetWindow() const
482 return m_targetWindow
;
485 // ----------------------------------------------------------------------------
486 // scrolling implementation itself
487 // ----------------------------------------------------------------------------
489 void wxScrollHelper
::HandleOnScroll(wxScrollWinEvent
& event
)
491 int nScrollInc
= CalcScrollInc(event
);
492 if ( nScrollInc
== 0 )
494 // can't scroll further
500 bool needsRefresh
= false;
503 int orient
= event
.GetOrientation();
504 if (orient
== wxHORIZONTAL
)
506 if ( m_xScrollingEnabled
)
508 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
517 if ( m_yScrollingEnabled
)
519 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
529 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
530 // otherwise invalidated area could be updated incorrectly later when
531 // ScrollWindow() makes sure they're repainted before scrolling them
533 // wxWindowMac is taking care of making sure the update area is correctly
534 // set up, while not forcing an immediate redraw
536 m_targetWindow
->Update();
540 if (orient
== wxHORIZONTAL
)
542 m_xScrollPosition
+= nScrollInc
;
543 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
547 m_yScrollPosition
+= nScrollInc
;
548 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
553 m_targetWindow
->Refresh(true, GetScrollRect());
557 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
561 int wxScrollHelper
::CalcScrollInc(wxScrollWinEvent
& event
)
563 int pos
= event
.GetPosition();
564 int orient
= event
.GetOrientation();
567 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
569 if (orient
== wxHORIZONTAL
)
570 nScrollInc
= - m_xScrollPosition
;
572 nScrollInc
= - m_yScrollPosition
;
574 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
576 if (orient
== wxHORIZONTAL
)
577 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
579 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
581 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
585 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
589 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
591 if (orient
== wxHORIZONTAL
)
592 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
594 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
596 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
598 if (orient
== wxHORIZONTAL
)
599 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
601 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
603 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
604 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
606 if (orient
== wxHORIZONTAL
)
607 nScrollInc
= pos
- m_xScrollPosition
;
609 nScrollInc
= pos
- m_yScrollPosition
;
612 if (orient
== wxHORIZONTAL
)
614 if ( m_xScrollPosition
+ nScrollInc
< 0 )
616 // As -ve as we can go
617 nScrollInc
= -m_xScrollPosition
;
619 else // check for the other bound
621 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
622 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
624 // As +ve as we can go
625 nScrollInc
= posMax
- m_xScrollPosition
;
631 if ( m_yScrollPosition
+ nScrollInc
< 0 )
633 // As -ve as we can go
634 nScrollInc
= -m_yScrollPosition
;
636 else // check for the other bound
638 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
639 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
641 // As +ve as we can go
642 nScrollInc
= posMax
- m_yScrollPosition
;
651 wxScrollHelper
::AdjustScrollbar(int orient
,
657 wxScrollbarVisibility visibility
)
659 if ( visibility
== wxSHOW_SB_NEVER
)
661 m_win
->SetScrollbar(orient
, 0, 0, 0);
665 // scroll lines per page: if 0, no scrolling is needed
668 // check if we need scrollbar in this direction at all
669 if ( pixelsPerUnit
== 0 ||
670 (clientSize
>= virtSize
&& visibility
!= wxSHOW_SB_ALWAYS
) )
672 // scrolling is disabled or unnecessary
677 else // might need scrolling
679 // Round up integer division to catch any "leftover" client space.
680 scrollUnits
= (virtSize
+ pixelsPerUnit
- 1) / pixelsPerUnit
;
682 // Calculate the number of fully scroll units
683 unitsPerPage
= clientSize
/ pixelsPerUnit
;
685 if (unitsPerPage
>= scrollUnits
)
687 // we're big enough to not need scrolling
692 else // we do need a scrollbar
694 if ( unitsPerPage
< 1 )
697 // Correct position if greater than extent of canvas minus
698 // the visible portion of it or if below zero
699 const int posMax
= scrollUnits
- unitsPerPage
;
700 if ( scrollPosition
> posMax
)
701 scrollPosition
= posMax
;
702 else if ( scrollPosition
< 0 )
707 m_win
->SetScrollbar(orient
, scrollPosition
, unitsPerPage
, scrollUnits
);
709 // The amount by which we scroll when paging
710 SetScrollPageSize(orient
, unitsPerPage
);
713 void wxScrollHelper
::AdjustScrollbars()
715 static wxRecursionGuardFlag s_flagReentrancy
;
716 wxRecursionGuard
guard(s_flagReentrancy
);
717 if ( guard
.IsInside() )
719 // don't reenter AdjustScrollbars() while another call to
720 // AdjustScrollbars() is in progress because this may lead to calling
721 // ScrollWindow() twice and this can really happen under MSW if
722 // SetScrollbar() call below adds or removes the scrollbar which
723 // changes the window size and hence results in another
724 // AdjustScrollbars() call
728 int oldXScroll
= m_xScrollPosition
;
729 int oldYScroll
= m_yScrollPosition
;
731 // we may need to readjust the scrollbars several times as enabling one of
732 // them reduces the area available for the window contents and so can make
733 // the other scrollbar necessary now although it wasn't necessary before
735 // VZ: normally this loop should be over in at most 2 iterations, I don't
736 // know why do we need 5 of them
737 for ( int iterationCount
= 0; iterationCount
< 5; iterationCount
++ )
739 wxSize clientSize
= GetTargetSize();
740 const wxSize virtSize
= m_targetWindow
->GetVirtualSize();
742 // this block of code tries to work around the following problem: the
743 // window could have been just resized to have enough space to show its
744 // full contents without the scrollbars, but its client size could be
745 // not big enough because it does have the scrollbars right now and so
746 // the scrollbars would remain even though we don't need them any more
748 // to prevent this from happening, check if we have enough space for
749 // everything without the scrollbars and explicitly disable them then
750 const wxSize availSize
= GetSizeAvailableForScrollTarget(
751 m_win
->GetSize() - m_win
->GetWindowBorderSize());
752 if ( availSize
!= clientSize
)
754 if ( availSize
.x
>= virtSize
.x
&& availSize
.y
>= virtSize
.y
)
756 // this will be enough to make the scrollbars disappear below
757 // and then the client size will indeed become equal to the
758 // full available size
759 clientSize
= availSize
;
764 AdjustScrollbar(wxHORIZONTAL
,
767 m_xScrollPixelsPerLine
,
772 AdjustScrollbar(wxVERTICAL
,
775 m_yScrollPixelsPerLine
,
781 // If a scrollbar (dis)appeared as a result of this, we need to adjust
782 // them again but if the client size didn't change, then we're done
783 if ( GetTargetSize() == clientSize
)
788 // Sorry, some Motif-specific code to implement a backing pixmap
789 // for the wxRETAINED style. Implementing a backing store can't
790 // be entirely generic because it relies on the wxWindowDC implementation
791 // to duplicate X drawing calls for the backing pixmap.
793 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
795 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
797 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
798 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
799 if (m_targetWindow
->GetBackingPixmap() &&
800 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
801 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
803 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
804 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
807 if (!m_targetWindow
->GetBackingPixmap() &&
808 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
810 int depth
= wxDisplayDepth();
811 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
812 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
813 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
814 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
820 if (oldXScroll
!= m_xScrollPosition
)
822 if (m_xScrollingEnabled
)
823 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
826 m_targetWindow
->Refresh(true, GetScrollRect());
829 if (oldYScroll
!= m_yScrollPosition
)
831 if (m_yScrollingEnabled
)
832 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
835 m_targetWindow
->Refresh(true, GetScrollRect());
839 void wxScrollHelper
::DoPrepareDC(wxDC
& dc
)
841 wxPoint pt
= dc
.GetDeviceOrigin();
843 // It may actually be correct to always query
844 // the m_sign from the DC here, but I leave the
845 // #ifdef GTK for now.
846 if (m_win
->GetLayoutDirection() == wxLayout_RightToLeft
)
847 dc
.SetDeviceOrigin( pt
.x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
,
848 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
851 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
852 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
853 dc
.SetUserScale( m_scaleX
, m_scaleY
);
856 void wxScrollHelper
::SetScrollRate( int xstep
, int ystep
)
858 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
859 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
861 m_xScrollPixelsPerLine
= xstep
;
862 m_yScrollPixelsPerLine
= ystep
;
864 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
865 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
867 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
868 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
869 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
874 void wxScrollHelper
::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
877 *x_unit
= m_xScrollPixelsPerLine
;
879 *y_unit
= m_yScrollPixelsPerLine
;
883 int wxScrollHelper
::GetScrollLines( int orient
) const
885 if ( orient
== wxHORIZONTAL
)
886 return m_xScrollLines
;
888 return m_yScrollLines
;
891 int wxScrollHelper
::GetScrollPageSize(int orient
) const
893 if ( orient
== wxHORIZONTAL
)
894 return m_xScrollLinesPerPage
;
896 return m_yScrollLinesPerPage
;
899 void wxScrollHelper
::SetScrollPageSize(int orient
, int pageSize
)
901 if ( orient
== wxHORIZONTAL
)
902 m_xScrollLinesPerPage
= pageSize
;
904 m_yScrollLinesPerPage
= pageSize
;
908 * Scroll to given position (scroll position, not pixel position)
910 void wxScrollHelper
::DoScroll( int x_pos
, int y_pos
)
915 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
916 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
919 GetTargetSize(&w
, &h
);
921 // compute new position:
922 int new_x
= m_xScrollPosition
;
923 int new_y
= m_yScrollPosition
;
925 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
929 // Calculate page size i.e. number of scroll units you get on the
930 // current client window
931 int noPagePositions
= w
/m_xScrollPixelsPerLine
;
932 if (noPagePositions
< 1) noPagePositions
= 1;
934 // Correct position if greater than extent of canvas minus
935 // the visible portion of it or if below zero
936 new_x
= wxMin( m_xScrollLines
-noPagePositions
, new_x
);
937 new_x
= wxMax( 0, new_x
);
939 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
943 // Calculate page size i.e. number of scroll units you get on the
944 // current client window
945 int noPagePositions
= h
/m_yScrollPixelsPerLine
;
946 if (noPagePositions
< 1) noPagePositions
= 1;
948 // Correct position if greater than extent of canvas minus
949 // the visible portion of it or if below zero
950 new_y
= wxMin( m_yScrollLines
-noPagePositions
, new_y
);
951 new_y
= wxMax( 0, new_y
);
954 if ( new_x
== m_xScrollPosition
&& new_y
== m_yScrollPosition
)
955 return; // nothing to do, the position didn't change
957 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
958 // otherwise invalidated area could be updated incorrectly later when
959 // ScrollWindow() makes sure they're repainted before scrolling them
960 m_targetWindow
->Update();
962 // update the position and scroll the window now:
963 if (m_xScrollPosition
!= new_x
)
965 int old_x
= m_xScrollPosition
;
966 m_xScrollPosition
= new_x
;
967 m_win
->SetScrollPos( wxHORIZONTAL
, new_x
);
968 m_targetWindow
->ScrollWindow( (old_x
-new_x
)*m_xScrollPixelsPerLine
, 0,
972 if (m_yScrollPosition
!= new_y
)
974 int old_y
= m_yScrollPosition
;
975 m_yScrollPosition
= new_y
;
976 m_win
->SetScrollPos( wxVERTICAL
, new_y
);
977 m_targetWindow
->ScrollWindow( 0, (old_y
-new_y
)*m_yScrollPixelsPerLine
,
982 void wxScrollHelper
::EnableScrolling (bool x_scroll
, bool y_scroll
)
984 m_xScrollingEnabled
= x_scroll
;
985 m_yScrollingEnabled
= y_scroll
;
988 void wxScrollHelper
::DoShowScrollbars(wxScrollbarVisibility horz
,
989 wxScrollbarVisibility vert
)
991 if ( horz
!= m_xVisibility
|| vert
!= m_yVisibility
)
993 m_xVisibility
= horz
;
994 m_yVisibility
= vert
;
1000 // Where the current view starts from
1001 void wxScrollHelper
::DoGetViewStart (int *x
, int *y
) const
1004 *x
= m_xScrollPosition
;
1006 *y
= m_yScrollPosition
;
1009 void wxScrollHelper
::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
1012 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
1014 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
1017 void wxScrollHelper
::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
1020 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
1022 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
1025 // ----------------------------------------------------------------------------
1027 // ----------------------------------------------------------------------------
1029 bool wxScrollHelper
::ScrollLayout()
1031 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
1033 // If we're the scroll target, take into account the
1034 // virtual size and scrolled position of the window.
1036 int x
= 0, y
= 0, w
= 0, h
= 0;
1037 CalcScrolledPosition(0,0, &x
,&y
);
1038 m_win
->GetVirtualSize(&w
, &h
);
1039 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
1043 // fall back to default for LayoutConstraints
1044 return m_win
->wxWindow
::Layout();
1047 void wxScrollHelper
::ScrollDoSetVirtualSize(int x
, int y
)
1049 m_win
->wxWindow
::DoSetVirtualSize( x
, y
);
1052 if (m_win
->GetAutoLayout())
1056 // wxWindow's GetBestVirtualSize returns the actual window size,
1057 // whereas we want to return the virtual size
1058 wxSize wxScrollHelper
::ScrollGetBestVirtualSize() const
1060 wxSize
clientSize(m_win
->GetClientSize());
1061 if ( m_win
->GetSizer() )
1062 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
1067 // ----------------------------------------------------------------------------
1069 // ----------------------------------------------------------------------------
1071 // Default OnSize resets scrollbars, if any
1072 void wxScrollHelper
::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
1074 if ( m_targetWindow
->GetAutoLayout() )
1076 wxSize size
= m_targetWindow
->GetBestVirtualSize();
1078 // This will call ::Layout() and ::AdjustScrollbars()
1079 m_win
->SetVirtualSize( size
);
1087 // This calls OnDraw, having adjusted the origin according to the current
1089 void wxScrollHelper
::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
1091 // don't use m_targetWindow here, this is always called for ourselves
1092 wxPaintDC
dc(m_win
);
1098 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1099 // compatibility here - if we used OnKeyDown(), the programs which process
1100 // arrows themselves in their OnChar() would never get the message and like
1101 // this they always have the priority
1102 void wxScrollHelper
::HandleOnChar(wxKeyEvent
& event
)
1104 int stx
= 0, sty
= 0, // view origin
1105 szx
= 0, szy
= 0, // view size (total)
1106 clix
= 0, cliy
= 0; // view size (on screen)
1108 GetViewStart(&stx
, &sty
);
1109 GetTargetSize(&clix
, &cliy
);
1110 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
1112 if( m_xScrollPixelsPerLine
)
1114 clix
/= m_xScrollPixelsPerLine
;
1115 szx
/= m_xScrollPixelsPerLine
;
1122 if( m_yScrollPixelsPerLine
)
1124 cliy
/= m_yScrollPixelsPerLine
;
1125 szy
/= m_yScrollPixelsPerLine
;
1133 int xScrollOld
= m_xScrollPosition
,
1134 yScrollOld
= m_yScrollPosition
;
1137 switch ( event
.GetKeyCode() )
1140 dsty
= sty
- (5 * cliy
/ 6);
1141 Scroll(-1, (dsty
== -1) ?
0 : dsty
);
1145 Scroll(-1, sty
+ (5 * cliy
/ 6));
1149 Scroll(0, event
.ControlDown() ?
0 : -1);
1153 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1157 Scroll(-1, sty
- 1);
1161 Scroll(-1, sty
+ 1);
1165 Scroll(stx
- 1, -1);
1169 Scroll(stx
+ 1, -1);
1177 if ( m_xScrollPosition
!= xScrollOld
)
1179 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1181 evt
.SetEventObject(m_win
);
1182 m_win
->GetEventHandler()->ProcessEvent(evt
);
1185 if ( m_yScrollPosition
!= yScrollOld
)
1187 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1189 evt
.SetEventObject(m_win
);
1190 m_win
->GetEventHandler()->ProcessEvent(evt
);
1194 // ----------------------------------------------------------------------------
1195 // autoscroll stuff: these functions deal with sending fake scroll events when
1196 // a captured mouse is being held outside the window
1197 // ----------------------------------------------------------------------------
1199 bool wxScrollHelper
::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1201 // only send the event if the window is scrollable in this direction
1202 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1203 return win
->HasScrollbar(event
.GetOrientation());
1206 void wxScrollHelper
::StopAutoScrolling()
1209 if ( m_timerAutoScroll
)
1211 delete m_timerAutoScroll
;
1212 m_timerAutoScroll
= (wxTimer
*)NULL
;
1217 void wxScrollHelper
::HandleOnMouseEnter(wxMouseEvent
& event
)
1219 StopAutoScrolling();
1224 void wxScrollHelper
::HandleOnMouseLeave(wxMouseEvent
& event
)
1226 // don't prevent the usual processing of the event from taking place
1229 // when a captured mouse leave a scrolled window we start generate
1230 // scrolling events to allow, for example, extending selection beyond the
1231 // visible area in some controls
1232 if ( wxWindow
::GetCapture() == m_targetWindow
)
1234 // where is the mouse leaving?
1236 wxPoint pt
= event
.GetPosition();
1239 orient
= wxHORIZONTAL
;
1242 else if ( pt
.y
< 0 )
1244 orient
= wxVERTICAL
;
1247 else // we're lower or to the right of the window
1249 wxSize size
= m_targetWindow
->GetClientSize();
1250 if ( pt
.x
> size
.x
)
1252 orient
= wxHORIZONTAL
;
1253 pos
= m_xScrollLines
;
1255 else if ( pt
.y
> size
.y
)
1257 orient
= wxVERTICAL
;
1258 pos
= m_yScrollLines
;
1260 else // this should be impossible
1262 // but seems to happen sometimes under wxMSW - maybe it's a bug
1263 // there but for now just ignore it
1265 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1271 // only start the auto scroll timer if the window can be scrolled in
1273 if ( !m_targetWindow
->HasScrollbar(orient
) )
1277 delete m_timerAutoScroll
;
1278 m_timerAutoScroll
= new wxAutoScrollTimer
1280 m_targetWindow
, this,
1281 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1282 : wxEVT_SCROLLWIN_LINEDOWN
,
1286 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1293 #if wxUSE_MOUSEWHEEL
1295 void wxScrollHelper
::HandleOnMouseWheel(wxMouseEvent
& event
)
1297 m_wheelRotation
+= event
.GetWheelRotation();
1298 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1299 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1304 wxScrollWinEvent newEvent
;
1306 newEvent
.SetPosition(0);
1307 newEvent
.SetOrientation( event
.GetWheelAxis() == 0 ? wxVERTICAL
: wxHORIZONTAL
);
1308 newEvent
.SetEventObject(m_win
);
1310 if (event
.IsPageScroll())
1313 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1315 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1317 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1321 lines
*= event
.GetLinesPerAction();
1323 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1325 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1327 int times
= abs(lines
);
1328 for (; times
> 0; times
--)
1329 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1334 #endif // wxUSE_MOUSEWHEEL
1336 void wxScrollHelper
::HandleOnChildFocus(wxChildFocusEvent
& event
)
1338 // this event should be processed by all windows in parenthood chain,
1339 // e.g. so that nested wxScrolledWindows work correctly
1342 // find the immediate child under which the window receiving focus is:
1343 wxWindow
*win
= event
.GetWindow();
1345 if ( win
== m_targetWindow
)
1346 return; // nothing to do
1349 if (wxDynamicCast(win
, wxScrollBar
))
1353 // Fixing ticket: http://trac.wxwidgets.org/ticket/9563
1354 // When a child inside a wxControlContainer receives a focus, the
1355 // wxControlContainer generates an artificial wxChildFocusEvent for
1356 // itself, telling its parent that 'it' received the focus. The effect is
1357 // that this->HandleOnChildFocus is called twice, first with the
1358 // artificial wxChildFocusEvent and then with the original event. We need
1359 // to ignore the artificial event here or otherwise HandleOnChildFocus
1360 // would first scroll the target window to make the entire
1361 // wxControlContainer visible and immediately afterwards scroll the target
1362 // window again to make the child widget visible. This leads to ugly
1363 // flickering when using nested wxPanels/wxScrolledWindows.
1365 // Ignore this event if 'win' is derived from wxControlContainer AND its
1366 // parent is the m_targetWindow AND 'win' is not actually reciving the
1367 // focus (win != FindFocus). TODO: This affects all wxControlContainer
1368 // objects, but wxControlContainer is not part of the wxWidgets RTTI and
1369 // so wxDynamicCast(win, wxControlContainer) does not compile. Find a way
1370 // to determine if 'win' derives from wxControlContainer. Until then,
1371 // testing if 'win' derives from wxPanel will probably get >90% of all
1374 wxWindow
*actual_focus
=wxWindow
::FindFocus();
1375 if (win
!= actual_focus
&&
1376 wxDynamicCast(win
, wxPanel
) != 0 &&
1377 win
->GetParent() == m_targetWindow
)
1378 // if win is a wxPanel and receives the focus, it should not be
1379 // scrolled into view
1382 const wxRect
viewRect(m_targetWindow
->GetClientRect());
1384 // For composite controls such as wxComboCtrl we should try to fit the
1385 // entire control inside the visible area of the target window, not just
1386 // the focused child of the control. Otherwise we'd make only the textctrl
1387 // part of a wxComboCtrl visible and the button would still be outside the
1388 // scrolled area. But do so only if the parent fits *entirely* inside the
1389 // scrolled window. In other situations, such as nested wxPanel or
1390 // wxScrolledWindows, the parent might be way to big to fit inside the
1391 // scrolled window. If that is the case, then make only the focused window
1393 if ( win
->GetParent() != m_targetWindow
)
1395 wxWindow
*parent
=win
->GetParent();
1396 wxSize parent_size
=parent
->GetSize();
1397 if (parent_size
.GetWidth() <= viewRect
.GetWidth() &&
1398 parent_size
.GetHeight() <= viewRect
.GetHeight())
1399 // make the immediate parent visible instead of the focused control
1403 // make win position relative to the m_targetWindow viewing area instead of
1406 winRect(m_targetWindow
->ScreenToClient(win
->GetScreenPosition()),
1409 // check if it's fully visible
1410 if ( viewRect
.Contains(winRect
) )
1412 // it is, nothing to do
1416 // check if we can make it fully visible: this is only possible if it's not
1417 // larger than our view area
1418 if ( winRect
.GetWidth() > viewRect
.GetWidth() ||
1419 winRect
.GetHeight() > viewRect
.GetHeight() )
1421 // we can't make it fit so avoid scrolling it at all, this is only
1422 // going to be confusing and not helpful
1427 // do make the window fit inside the view area by scrolling to it
1429 GetScrollPixelsPerUnit(&stepx
, &stepy
);
1432 GetViewStart(&startx
, &starty
);
1434 // first in vertical direction:
1439 if ( winRect
.GetTop() < 0 )
1441 diff
= winRect
.GetTop();
1443 else if ( winRect
.GetBottom() > viewRect
.GetHeight() )
1445 diff
= winRect
.GetBottom() - viewRect
.GetHeight() + 1;
1446 // round up to next scroll step if we can't get exact position,
1447 // so that the window is fully visible:
1451 starty
= (starty
* stepy
+ diff
) / stepy
;
1459 if ( winRect
.GetLeft() < 0 )
1461 diff
= winRect
.GetLeft();
1463 else if ( winRect
.GetRight() > viewRect
.GetWidth() )
1465 diff
= winRect
.GetRight() - viewRect
.GetWidth() + 1;
1466 // round up to next scroll step if we can't get exact position,
1467 // so that the window is fully visible:
1471 startx
= (startx
* stepx
+ diff
) / stepx
;
1474 Scroll(startx
, starty
);
1477 // ----------------------------------------------------------------------------
1478 // wxScrolled<T> and wxScrolledWindow implementation
1479 // ----------------------------------------------------------------------------
1481 wxSize wxScrolledT_Helper
::FilterBestSize(const wxWindow
*win
,
1482 const wxScrollHelperNative
*helper
,
1483 const wxSize
& origBest
)
1485 // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1486 // all scrollable windows should behave like this, only those that
1487 // contain children controls within scrollable area
1488 // (i.e., wxScrolledWindow) and other some scrollable windows may
1489 // have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1491 wxSize best
= origBest
;
1493 if ( win
->GetAutoLayout() )
1495 // Only use the content to set the window size in the direction
1496 // where there's no scrolling; otherwise we're going to get a huge
1497 // window in the direction in which scrolling is enabled
1499 helper
->GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1501 // NB: This code used to use *current* size if min size wasn't
1502 // specified, presumably to get some reasonable (i.e., larger than
1503 // minimal) size. But that's a wrong thing to do in GetBestSize(),
1504 // so we use minimal size as specified. If the app needs some
1505 // minimal size for its scrolled window, it should set it and put
1506 // the window into sizer as expandable so that it can use all space
1509 // See also http://svn.wxwidgets.org/viewvc/wx?view=rev&revision=45864
1511 wxSize minSize
= win
->GetMinSize();
1514 best
.x
= minSize
.x
+ wxSystemSettings
::GetMetric(wxSYS_VSCROLL_X
);
1517 best
.y
= minSize
.y
+ wxSystemSettings
::GetMetric(wxSYS_HSCROLL_Y
);
1524 WXLRESULT wxScrolledT_Helper
::FilterMSWWindowProc(WXUINT nMsg
, WXLRESULT rc
)
1527 // we need to process arrows ourselves for scrolling
1528 if ( nMsg
== WM_GETDLGCODE
)
1530 rc
|= DLGC_WANTARROWS
;
1537 // NB: skipping wxScrolled<T> in wxRTTI information because being a templte,
1538 // it doesn't and can't implement wxRTTI support
1539 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)