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;
333 m_targetWindow
= (wxWindow
*)NULL
;
335 m_timerAutoScroll
= (wxTimer
*)NULL
;
341 m_win
->SetScrollHelper( this );
343 // by default, the associated window is also the target window
344 DoSetTargetWindow(win
);
347 wxScrollHelper::~wxScrollHelper()
354 // ----------------------------------------------------------------------------
355 // setting scrolling parameters
356 // ----------------------------------------------------------------------------
358 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
368 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
371 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
372 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
374 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
375 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
376 (xPos
!= m_xScrollPosition
) ||
377 (yPos
!= m_yScrollPosition
)
380 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
381 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
382 m_xScrollPosition
= xPos
;
383 m_yScrollPosition
= yPos
;
385 int w
= noUnitsX
* pixelsPerUnitX
;
386 int h
= noUnitsY
* pixelsPerUnitY
;
388 // For better backward compatibility we set persisting limits
389 // here not just the size. It makes SetScrollbars 'sticky'
390 // emulating the old non-autoscroll behaviour.
391 // m_targetWindow->SetVirtualSizeHints( w, h );
393 // The above should arguably be deprecated, this however we still need.
395 // take care not to set 0 virtual size, 0 means that we don't have any
396 // scrollbars and hence we should use the real size instead of the virtual
397 // one which is indicated by using wxDefaultCoord
398 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
399 h
? h
: wxDefaultCoord
);
401 if (do_refresh
&& !noRefresh
)
402 m_targetWindow
->Refresh(true, GetScrollRect());
404 #ifndef __WXUNIVERSAL__
405 // If the target is not the same as the window with the scrollbars,
406 // then we need to update the scrollbars here, since they won't have
407 // been updated by SetVirtualSize().
408 if ( m_targetWindow
!= m_win
)
409 #endif // !__WXUNIVERSAL__
413 #ifndef __WXUNIVERSAL__
416 // otherwise this has been done by AdjustScrollbars, above
418 #endif // !__WXUNIVERSAL__
421 // ----------------------------------------------------------------------------
422 // [target] window handling
423 // ----------------------------------------------------------------------------
425 void wxScrollHelper::DeleteEvtHandler()
427 // search for m_handler in the handler list
428 if ( m_win
&& m_handler
)
430 if ( m_win
->RemoveEventHandler(m_handler
) )
434 //else: something is very wrong, so better [maybe] leak memory than
435 // risk a crash because of double deletion
441 void wxScrollHelper::ResetDrawnFlag()
443 wxCHECK_RET( m_handler
, "invalid use of ResetDrawnFlag - no handler?" );
444 m_handler
->ResetDrawnFlag();
447 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
449 m_targetWindow
= target
;
451 target
->MacSetClipChildren( true ) ;
454 // install the event handler which will intercept the events we're
455 // interested in (but only do it for our real window, not the target window
456 // which we scroll - we don't need to hijack its events)
457 if ( m_targetWindow
== m_win
)
459 // if we already have a handler, delete it first
462 m_handler
= new wxScrollHelperEvtHandler(this);
463 m_targetWindow
->PushEventHandler(m_handler
);
467 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
469 wxCHECK_RET( target
, wxT("target window must not be NULL") );
471 if ( target
== m_targetWindow
)
474 DoSetTargetWindow(target
);
477 wxWindow
*wxScrollHelper::GetTargetWindow() const
479 return m_targetWindow
;
482 // ----------------------------------------------------------------------------
483 // scrolling implementation itself
484 // ----------------------------------------------------------------------------
486 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
488 int nScrollInc
= CalcScrollInc(event
);
489 if ( nScrollInc
== 0 )
491 // can't scroll further
497 bool needsRefresh
= false;
500 int orient
= event
.GetOrientation();
501 if (orient
== wxHORIZONTAL
)
503 if ( m_xScrollingEnabled
)
505 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
514 if ( m_yScrollingEnabled
)
516 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
526 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
527 // otherwise invalidated area could be updated incorrectly later when
528 // ScrollWindow() makes sure they're repainted before scrolling them
530 // wxWindowMac is taking care of making sure the update area is correctly
531 // set up, while not forcing an immediate redraw
533 m_targetWindow
->Update();
537 if (orient
== wxHORIZONTAL
)
539 m_xScrollPosition
+= nScrollInc
;
540 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
544 m_yScrollPosition
+= nScrollInc
;
545 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
550 m_targetWindow
->Refresh(true, GetScrollRect());
554 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
558 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
560 int pos
= event
.GetPosition();
561 int orient
= event
.GetOrientation();
564 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
566 if (orient
== wxHORIZONTAL
)
567 nScrollInc
= - m_xScrollPosition
;
569 nScrollInc
= - m_yScrollPosition
;
571 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
573 if (orient
== wxHORIZONTAL
)
574 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
576 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
578 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
582 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
586 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
588 if (orient
== wxHORIZONTAL
)
589 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
591 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
593 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
595 if (orient
== wxHORIZONTAL
)
596 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
598 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
600 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
601 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
603 if (orient
== wxHORIZONTAL
)
604 nScrollInc
= pos
- m_xScrollPosition
;
606 nScrollInc
= pos
- m_yScrollPosition
;
609 if (orient
== wxHORIZONTAL
)
611 if ( m_xScrollPosition
+ nScrollInc
< 0 )
613 // As -ve as we can go
614 nScrollInc
= -m_xScrollPosition
;
616 else // check for the other bound
618 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
619 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
621 // As +ve as we can go
622 nScrollInc
= posMax
- m_xScrollPosition
;
628 if ( m_yScrollPosition
+ nScrollInc
< 0 )
630 // As -ve as we can go
631 nScrollInc
= -m_yScrollPosition
;
633 else // check for the other bound
635 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
636 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
638 // As +ve as we can go
639 nScrollInc
= posMax
- m_yScrollPosition
;
648 wxScrollHelper::AdjustScrollbar(int orient
,
655 // scroll lines per page: if 0, no scrolling is needed
658 // check if we need scrollbar in this direction at all
659 if ( pixelsPerUnit
== 0 || clientSize
>= virtSize
)
661 // scrolling is disabled or unnecessary
666 else // might need scrolling
668 // Round up integer division to catch any "leftover" client space.
669 scrollUnits
= (virtSize
+ pixelsPerUnit
- 1) / pixelsPerUnit
;
671 // Calculate the number of fully scroll units
672 unitsPerPage
= clientSize
/ pixelsPerUnit
;
674 if (unitsPerPage
>= scrollUnits
)
676 // we're big enough to not need scrolling
681 else // we do need a scrollbar
683 if ( unitsPerPage
< 1 )
686 // Correct position if greater than extent of canvas minus
687 // the visible portion of it or if below zero
688 const int posMax
= scrollUnits
- unitsPerPage
;
689 if ( scrollPosition
> posMax
)
690 scrollPosition
= posMax
;
691 else if ( scrollPosition
< 0 )
696 m_win
->SetScrollbar(orient
, scrollPosition
, unitsPerPage
, scrollUnits
);
698 // The amount by which we scroll when paging
699 SetScrollPageSize(orient
, unitsPerPage
);
702 void wxScrollHelper::AdjustScrollbars()
704 static wxRecursionGuardFlag s_flagReentrancy
;
705 wxRecursionGuard
guard(s_flagReentrancy
);
706 if ( guard
.IsInside() )
708 // don't reenter AdjustScrollbars() while another call to
709 // AdjustScrollbars() is in progress because this may lead to calling
710 // ScrollWindow() twice and this can really happen under MSW if
711 // SetScrollbar() call below adds or removes the scrollbar which
712 // changes the window size and hence results in another
713 // AdjustScrollbars() call
717 int oldXScroll
= m_xScrollPosition
;
718 int oldYScroll
= m_yScrollPosition
;
720 // we may need to readjust the scrollbars several times as enabling one of
721 // them reduces the area available for the window contents and so can make
722 // the other scrollbar necessary now although it wasn't necessary before
724 // VZ: normally this loop should be over in at most 2 iterations, I don't
725 // know why do we need 5 of them
726 for ( int iterationCount
= 0; iterationCount
< 5; iterationCount
++ )
728 wxSize clientSize
= GetTargetSize();
729 const wxSize virtSize
= m_targetWindow
->GetVirtualSize();
731 // this block of code tries to work around the following problem: the
732 // window could have been just resized to have enough space to show its
733 // full contents without the scrollbars, but its client size could be
734 // not big enough because it does have the scrollbars right now and so
735 // the scrollbars would remain even though we don't need them any more
737 // to prevent this from happening, check if we have enough space for
738 // everything without the scrollbars and explicitly disable them then
739 const wxSize availSize
= GetSizeAvailableForScrollTarget(
740 m_win
->GetSize() - m_win
->GetWindowBorderSize());
741 if ( availSize
!= clientSize
)
743 if ( availSize
.x
>= virtSize
.x
&& availSize
.y
>= virtSize
.y
)
745 // this will be enough to make the scrollbars disappear below
746 // and then the client size will indeed become equal to the
747 // full available size
748 clientSize
= availSize
;
753 AdjustScrollbar(wxHORIZONTAL
,
756 m_xScrollPixelsPerLine
,
760 AdjustScrollbar(wxVERTICAL
,
763 m_yScrollPixelsPerLine
,
768 // If a scrollbar (dis)appeared as a result of this, we need to adjust
769 // them again but if the client size didn't change, then we're done
770 if ( GetTargetSize() == clientSize
)
775 // Sorry, some Motif-specific code to implement a backing pixmap
776 // for the wxRETAINED style. Implementing a backing store can't
777 // be entirely generic because it relies on the wxWindowDC implementation
778 // to duplicate X drawing calls for the backing pixmap.
780 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
782 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
784 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
785 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
786 if (m_targetWindow
->GetBackingPixmap() &&
787 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
788 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
790 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
791 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
794 if (!m_targetWindow
->GetBackingPixmap() &&
795 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
797 int depth
= wxDisplayDepth();
798 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
799 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
800 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
801 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
807 if (oldXScroll
!= m_xScrollPosition
)
809 if (m_xScrollingEnabled
)
810 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
813 m_targetWindow
->Refresh(true, GetScrollRect());
816 if (oldYScroll
!= m_yScrollPosition
)
818 if (m_yScrollingEnabled
)
819 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
822 m_targetWindow
->Refresh(true, GetScrollRect());
826 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
828 wxPoint pt
= dc
.GetDeviceOrigin();
830 // It may actually be correct to always query
831 // the m_sign from the DC here, but I leave the
832 // #ifdef GTK for now.
833 if (m_win
->GetLayoutDirection() == wxLayout_RightToLeft
)
834 dc
.SetDeviceOrigin( pt
.x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
,
835 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
838 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
839 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
840 dc
.SetUserScale( m_scaleX
, m_scaleY
);
843 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
845 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
846 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
848 m_xScrollPixelsPerLine
= xstep
;
849 m_yScrollPixelsPerLine
= ystep
;
851 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
852 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
854 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
855 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
856 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
861 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
864 *x_unit
= m_xScrollPixelsPerLine
;
866 *y_unit
= m_yScrollPixelsPerLine
;
870 int wxScrollHelper::GetScrollLines( int orient
) const
872 if ( orient
== wxHORIZONTAL
)
873 return m_xScrollLines
;
875 return m_yScrollLines
;
878 int wxScrollHelper::GetScrollPageSize(int orient
) const
880 if ( orient
== wxHORIZONTAL
)
881 return m_xScrollLinesPerPage
;
883 return m_yScrollLinesPerPage
;
886 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
888 if ( orient
== wxHORIZONTAL
)
889 m_xScrollLinesPerPage
= pageSize
;
891 m_yScrollLinesPerPage
= pageSize
;
895 * Scroll to given position (scroll position, not pixel position)
897 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
902 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
903 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
906 GetTargetSize(&w
, &h
);
908 // compute new position:
909 int new_x
= m_xScrollPosition
;
910 int new_y
= m_yScrollPosition
;
912 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
916 // Calculate page size i.e. number of scroll units you get on the
917 // current client window
918 int noPagePositions
= w
/m_xScrollPixelsPerLine
;
919 if (noPagePositions
< 1) noPagePositions
= 1;
921 // Correct position if greater than extent of canvas minus
922 // the visible portion of it or if below zero
923 new_x
= wxMin( m_xScrollLines
-noPagePositions
, new_x
);
924 new_x
= wxMax( 0, new_x
);
926 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
930 // Calculate page size i.e. number of scroll units you get on the
931 // current client window
932 int noPagePositions
= h
/m_yScrollPixelsPerLine
;
933 if (noPagePositions
< 1) noPagePositions
= 1;
935 // Correct position if greater than extent of canvas minus
936 // the visible portion of it or if below zero
937 new_y
= wxMin( m_yScrollLines
-noPagePositions
, new_y
);
938 new_y
= wxMax( 0, new_y
);
941 if ( new_x
== m_xScrollPosition
&& new_y
== m_yScrollPosition
)
942 return; // nothing to do, the position didn't change
944 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
945 // otherwise invalidated area could be updated incorrectly later when
946 // ScrollWindow() makes sure they're repainted before scrolling them
947 m_targetWindow
->Update();
949 // update the position and scroll the window now:
950 if (m_xScrollPosition
!= new_x
)
952 int old_x
= m_xScrollPosition
;
953 m_xScrollPosition
= new_x
;
954 m_win
->SetScrollPos( wxHORIZONTAL
, new_x
);
955 m_targetWindow
->ScrollWindow( (old_x
-new_x
)*m_xScrollPixelsPerLine
, 0,
959 if (m_yScrollPosition
!= new_y
)
961 int old_y
= m_yScrollPosition
;
962 m_yScrollPosition
= new_y
;
963 m_win
->SetScrollPos( wxVERTICAL
, new_y
);
964 m_targetWindow
->ScrollWindow( 0, (old_y
-new_y
)*m_yScrollPixelsPerLine
,
969 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
971 m_xScrollingEnabled
= x_scroll
;
972 m_yScrollingEnabled
= y_scroll
;
975 // Where the current view starts from
976 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
979 *x
= m_xScrollPosition
;
981 *y
= m_yScrollPosition
;
984 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
987 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
989 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
992 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
995 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
997 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
1000 // ----------------------------------------------------------------------------
1002 // ----------------------------------------------------------------------------
1004 bool wxScrollHelper::ScrollLayout()
1006 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
1008 // If we're the scroll target, take into account the
1009 // virtual size and scrolled position of the window.
1011 int x
= 0, y
= 0, w
= 0, h
= 0;
1012 CalcScrolledPosition(0,0, &x
,&y
);
1013 m_win
->GetVirtualSize(&w
, &h
);
1014 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
1018 // fall back to default for LayoutConstraints
1019 return m_win
->wxWindow::Layout();
1022 void wxScrollHelper::ScrollDoSetVirtualSize(int x
, int y
)
1024 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
1027 if (m_win
->GetAutoLayout())
1031 // wxWindow's GetBestVirtualSize returns the actual window size,
1032 // whereas we want to return the virtual size
1033 wxSize
wxScrollHelper::ScrollGetBestVirtualSize() const
1035 wxSize
clientSize(m_win
->GetClientSize());
1036 if ( m_win
->GetSizer() )
1037 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
1042 // ----------------------------------------------------------------------------
1044 // ----------------------------------------------------------------------------
1046 // Default OnSize resets scrollbars, if any
1047 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
1049 if ( m_targetWindow
->GetAutoLayout() )
1051 wxSize size
= m_targetWindow
->GetBestVirtualSize();
1053 // This will call ::Layout() and ::AdjustScrollbars()
1054 m_win
->SetVirtualSize( size
);
1062 // This calls OnDraw, having adjusted the origin according to the current
1064 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
1066 // don't use m_targetWindow here, this is always called for ourselves
1067 wxPaintDC
dc(m_win
);
1073 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1074 // compatibility here - if we used OnKeyDown(), the programs which process
1075 // arrows themselves in their OnChar() would never get the message and like
1076 // this they always have the priority
1077 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
1079 int stx
= 0, sty
= 0, // view origin
1080 szx
= 0, szy
= 0, // view size (total)
1081 clix
= 0, cliy
= 0; // view size (on screen)
1083 GetViewStart(&stx
, &sty
);
1084 GetTargetSize(&clix
, &cliy
);
1085 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
1087 if( m_xScrollPixelsPerLine
)
1089 clix
/= m_xScrollPixelsPerLine
;
1090 szx
/= m_xScrollPixelsPerLine
;
1097 if( m_yScrollPixelsPerLine
)
1099 cliy
/= m_yScrollPixelsPerLine
;
1100 szy
/= m_yScrollPixelsPerLine
;
1108 int xScrollOld
= m_xScrollPosition
,
1109 yScrollOld
= m_yScrollPosition
;
1112 switch ( event
.GetKeyCode() )
1115 dsty
= sty
- (5 * cliy
/ 6);
1116 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1120 Scroll(-1, sty
+ (5 * cliy
/ 6));
1124 Scroll(0, event
.ControlDown() ? 0 : -1);
1128 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1132 Scroll(-1, sty
- 1);
1136 Scroll(-1, sty
+ 1);
1140 Scroll(stx
- 1, -1);
1144 Scroll(stx
+ 1, -1);
1152 if ( m_xScrollPosition
!= xScrollOld
)
1154 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1156 evt
.SetEventObject(m_win
);
1157 m_win
->GetEventHandler()->ProcessEvent(evt
);
1160 if ( m_yScrollPosition
!= yScrollOld
)
1162 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1164 evt
.SetEventObject(m_win
);
1165 m_win
->GetEventHandler()->ProcessEvent(evt
);
1169 // ----------------------------------------------------------------------------
1170 // autoscroll stuff: these functions deal with sending fake scroll events when
1171 // a captured mouse is being held outside the window
1172 // ----------------------------------------------------------------------------
1174 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1176 // only send the event if the window is scrollable in this direction
1177 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1178 return win
->HasScrollbar(event
.GetOrientation());
1181 void wxScrollHelper::StopAutoScrolling()
1184 if ( m_timerAutoScroll
)
1186 delete m_timerAutoScroll
;
1187 m_timerAutoScroll
= (wxTimer
*)NULL
;
1192 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1194 StopAutoScrolling();
1199 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1201 // don't prevent the usual processing of the event from taking place
1204 // when a captured mouse leave a scrolled window we start generate
1205 // scrolling events to allow, for example, extending selection beyond the
1206 // visible area in some controls
1207 if ( wxWindow::GetCapture() == m_targetWindow
)
1209 // where is the mouse leaving?
1211 wxPoint pt
= event
.GetPosition();
1214 orient
= wxHORIZONTAL
;
1217 else if ( pt
.y
< 0 )
1219 orient
= wxVERTICAL
;
1222 else // we're lower or to the right of the window
1224 wxSize size
= m_targetWindow
->GetClientSize();
1225 if ( pt
.x
> size
.x
)
1227 orient
= wxHORIZONTAL
;
1228 pos
= m_xScrollLines
;
1230 else if ( pt
.y
> size
.y
)
1232 orient
= wxVERTICAL
;
1233 pos
= m_yScrollLines
;
1235 else // this should be impossible
1237 // but seems to happen sometimes under wxMSW - maybe it's a bug
1238 // there but for now just ignore it
1240 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1246 // only start the auto scroll timer if the window can be scrolled in
1248 if ( !m_targetWindow
->HasScrollbar(orient
) )
1252 delete m_timerAutoScroll
;
1253 m_timerAutoScroll
= new wxAutoScrollTimer
1255 m_targetWindow
, this,
1256 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1257 : wxEVT_SCROLLWIN_LINEDOWN
,
1261 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1268 #if wxUSE_MOUSEWHEEL
1270 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1272 m_wheelRotation
+= event
.GetWheelRotation();
1273 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1274 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1279 wxScrollWinEvent newEvent
;
1281 newEvent
.SetPosition(0);
1282 newEvent
.SetOrientation( event
.GetWheelAxis() == 0 ? wxVERTICAL
: wxHORIZONTAL
);
1283 newEvent
.SetEventObject(m_win
);
1285 if (event
.IsPageScroll())
1288 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1290 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1292 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1296 lines
*= event
.GetLinesPerAction();
1298 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1300 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1302 int times
= abs(lines
);
1303 for (; times
> 0; times
--)
1304 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1309 #endif // wxUSE_MOUSEWHEEL
1311 void wxScrollHelper::HandleOnChildFocus(wxChildFocusEvent
& event
)
1313 // this event should be processed by all windows in parenthood chain,
1314 // e.g. so that nested wxScrolledWindows work correctly
1317 // find the immediate child under which the window receiving focus is:
1318 wxWindow
*win
= event
.GetWindow();
1320 if ( win
== m_targetWindow
)
1321 return; // nothing to do
1324 if (wxDynamicCast(win
, wxScrollBar
))
1328 // Fixing ticket: http://trac.wxwidgets.org/ticket/9563
1329 // When a child inside a wxControlContainer receives a focus, the
1330 // wxControlContainer generates an artificial wxChildFocusEvent for
1331 // itself, telling its parent that 'it' received the focus. The effect is
1332 // that this->HandleOnChildFocus is called twice, first with the
1333 // artificial wxChildFocusEvent and then with the original event. We need
1334 // to ignore the artificial event here or otherwise HandleOnChildFocus
1335 // would first scroll the target window to make the entire
1336 // wxControlContainer visible and immediately afterwards scroll the target
1337 // window again to make the child widget visible. This leads to ugly
1338 // flickering when using nested wxPanels/wxScrolledWindows.
1340 // Ignore this event if 'win' is derived from wxControlContainer AND its
1341 // parent is the m_targetWindow AND 'win' is not actually reciving the
1342 // focus (win != FindFocus). TODO: This affects all wxControlContainer
1343 // objects, but wxControlContainer is not part of the wxWidgets RTTI and
1344 // so wxDynamicCast(win, wxControlContainer) does not compile. Find a way
1345 // to determine if 'win' derives from wxControlContainer. Until then,
1346 // testing if 'win' derives from wxPanel will probably get >90% of all
1349 wxWindow
*actual_focus
=wxWindow::FindFocus();
1350 if (win
!= actual_focus
&&
1351 wxDynamicCast(win
, wxPanel
) != 0 &&
1352 win
->GetParent() == m_targetWindow
)
1353 // if win is a wxPanel and receives the focus, it should not be
1354 // scrolled into view
1357 wxSize
view(m_targetWindow
->GetClientSize());
1359 // For composite controls such as wxComboCtrl we should try to fit the
1360 // entire control inside the visible area of the target window, not just
1361 // the focused child of the control. Otherwise we'd make only the textctrl
1362 // part of a wxComboCtrl visible and the button would still be outside the
1363 // scrolled area. But do so only if the parent fits *entirely* inside the
1364 // scrolled window. In other situations, such as nested wxPanel or
1365 // wxScrolledWindows, the parent might be way to big to fit inside the
1366 // scrolled window. If that is the case, then make only the focused window
1368 if ( win
->GetParent() != m_targetWindow
)
1370 wxWindow
*parent
=win
->GetParent();
1371 wxSize parent_size
=parent
->GetSize();
1372 if (parent_size
.GetWidth() <= view
.GetWidth() &&
1373 parent_size
.GetHeight() <= view
.GetHeight())
1374 // make the immediate parent visible instead of the focused control
1378 // if the child is not fully visible, try to scroll it into view:
1380 GetScrollPixelsPerUnit(&stepx
, &stepy
);
1382 // 'win' position coordinates are relative to it's parent
1383 // convert them so that they are relative to the m_targetWindow viewing area
1384 wxRect
winrect(m_targetWindow
->ScreenToClient(win
->GetScreenPosition()),
1388 GetViewStart(&startx
, &starty
);
1390 // first in vertical direction:
1395 if ( winrect
.GetTop() < 0 )
1397 diff
= winrect
.GetTop();
1399 else if ( winrect
.GetBottom() > view
.y
)
1401 diff
= winrect
.GetBottom() - view
.y
+ 1;
1402 // round up to next scroll step if we can't get exact position,
1403 // so that the window is fully visible:
1407 starty
= (starty
* stepy
+ diff
) / stepy
;
1415 if ( winrect
.GetLeft() < 0 )
1417 diff
= winrect
.GetLeft();
1419 else if ( winrect
.GetRight() > view
.x
)
1421 diff
= winrect
.GetRight() - view
.x
+ 1;
1422 // round up to next scroll step if we can't get exact position,
1423 // so that the window is fully visible:
1427 startx
= (startx
* stepx
+ diff
) / stepx
;
1430 Scroll(startx
, starty
);
1433 // ----------------------------------------------------------------------------
1434 // wxScrolled<T> and wxScrolledWindow implementation
1435 // ----------------------------------------------------------------------------
1437 wxSize
wxScrolledT_Helper::FilterBestSize(const wxWindow
*win
,
1438 const wxScrollHelperNative
*helper
,
1439 const wxSize
& origBest
)
1441 // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1442 // all scrollable windows should behave like this, only those that
1443 // contain children controls within scrollable area
1444 // (i.e., wxScrolledWindow) and other some scrollable windows may
1445 // have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1447 wxSize best
= origBest
;
1449 if ( win
->GetAutoLayout() )
1451 // Only use the content to set the window size in the direction
1452 // where there's no scrolling; otherwise we're going to get a huge
1453 // window in the direction in which scrolling is enabled
1455 helper
->GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1457 // NB: This code used to use *current* size if min size wasn't
1458 // specified, presumably to get some reasonable (i.e., larger than
1459 // minimal) size. But that's a wrong thing to do in GetBestSize(),
1460 // so we use minimal size as specified. If the app needs some
1461 // minimal size for its scrolled window, it should set it and put
1462 // the window into sizer as expandable so that it can use all space
1465 // See also http://svn.wxwidgets.org/viewvc/wx?view=rev&revision=45864
1467 wxSize minSize
= win
->GetMinSize();
1470 best
.x
= minSize
.x
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1473 best
.y
= minSize
.y
+ wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
1480 WXLRESULT
wxScrolledT_Helper::FilterMSWWindowProc(WXUINT nMsg
, WXLRESULT rc
)
1483 // we need to process arrows ourselves for scrolling
1484 if ( nMsg
== WM_GETDLGCODE
)
1486 rc
|= DLGC_WANTARROWS
;
1493 // NB: skipping wxScrolled<T> in wxRTTI information because being a templte,
1494 // it doesn't and can't implement wxRTTI support
1495 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)