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 // ----------------------------------------------------------------------------
22 #define XtDisplay XTDISPLAY
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
32 #include "wx/scrolwin.h"
37 #include "wx/dcclient.h"
44 #include "wx/recguard.h"
47 #include <windows.h> // for DLGC_WANTARROWS
48 #include "wx/msw/winundef.h"
52 // For wxRETAINED implementation
53 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
54 //This code switches off the compiler warnings
55 # pragma message disable nosimpint
59 # pragma message enable nosimpint
65 style wxHSCROLL | wxVSCROLL
68 // ----------------------------------------------------------------------------
69 // wxScrollHelperEvtHandler: intercept the events from the window and forward
70 // them to wxScrollHelper
71 // ----------------------------------------------------------------------------
73 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
76 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
78 m_scrollHelper
= scrollHelper
;
81 virtual bool ProcessEvent(wxEvent
& event
);
83 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
86 wxScrollHelper
*m_scrollHelper
;
88 bool m_hasDrawnWindow
;
90 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
94 // ----------------------------------------------------------------------------
95 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
96 // a captured mouse is held outside the window
97 // ----------------------------------------------------------------------------
99 class wxAutoScrollTimer
: public wxTimer
102 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
103 wxEventType eventTypeToSend
,
104 int pos
, int orient
);
106 virtual void Notify();
110 wxScrollHelper
*m_scrollHelper
;
111 wxEventType m_eventType
;
115 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
118 // ============================================================================
120 // ============================================================================
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
127 wxScrollHelper
*scroll
,
128 wxEventType eventTypeToSend
,
132 m_scrollHelper
= scroll
;
133 m_eventType
= eventTypeToSend
;
138 void wxAutoScrollTimer::Notify()
140 // only do all this as long as the window is capturing the mouse
141 if ( wxWindow::GetCapture() != m_win
)
145 else // we still capture the mouse, continue generating events
147 // first scroll the window if we are allowed to do it
148 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
149 event1
.SetEventObject(m_win
);
150 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
151 m_win
->GetEventHandler()->ProcessEvent(event1
) )
153 // and then send a pseudo mouse-move event to refresh the selection
154 wxMouseEvent
event2(wxEVT_MOTION
);
155 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
157 // the mouse event coordinates should be client, not screen as
158 // returned by wxGetMousePosition
159 wxWindow
*parentTop
= m_win
;
160 while ( parentTop
->GetParent() )
161 parentTop
= parentTop
->GetParent();
162 wxPoint ptOrig
= parentTop
->GetPosition();
163 event2
.m_x
-= ptOrig
.x
;
164 event2
.m_y
-= ptOrig
.y
;
166 event2
.SetEventObject(m_win
);
168 // FIXME: we don't fill in the other members - ok?
170 m_win
->GetEventHandler()->ProcessEvent(event2
);
172 else // can't scroll further, stop
180 // ----------------------------------------------------------------------------
181 // wxScrollHelperEvtHandler
182 // ----------------------------------------------------------------------------
184 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
186 wxEventType evType
= event
.GetEventType();
188 // the explanation of wxEVT_PAINT processing hack: for historic reasons
189 // there are 2 ways to process this event in classes deriving from
190 // wxScrolledWindow. The user code may
192 // 1. override wxScrolledWindow::OnDraw(dc)
193 // 2. define its own OnPaint() handler
195 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
196 // always processes the draw event, so we can't just try the window
197 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
198 // (the latter would never be called in wxUniversal).
200 // So the solution is to have a flag telling us whether the user code drew
201 // anything in the window. We set it to true here but reset it to false in
202 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
203 // user code defined OnPaint() in the derived class)
204 m_hasDrawnWindow
= true;
206 // pass it on to the real handler
207 bool processed
= wxEvtHandler::ProcessEvent(event
);
209 // always process the size events ourselves, even if the user code handles
210 // them as well, as we need to AdjustScrollbars()
212 // NB: it is important to do it after processing the event in the normal
213 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
214 // scrollbar[s] (dis)appear and it should be seen by the user code
216 if ( evType
== wxEVT_SIZE
)
218 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
225 // normally, nothing more to do here - except if it was a paint event
226 // which wasn't really processed, then we'll try to call our
227 // OnDraw() below (from HandleOnPaint)
228 if ( m_hasDrawnWindow
|| event
.IsCommandEvent() )
234 // reset the skipped flag to false as it might have been set to true in
235 // ProcessEvent() above
238 if ( evType
== wxEVT_PAINT
)
240 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
244 if ( evType
== wxEVT_SCROLLWIN_TOP
||
245 evType
== wxEVT_SCROLLWIN_BOTTOM
||
246 evType
== wxEVT_SCROLLWIN_LINEUP
||
247 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
248 evType
== wxEVT_SCROLLWIN_PAGEUP
||
249 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
250 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
251 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
253 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
254 return !event
.GetSkipped();
257 if ( evType
== wxEVT_ENTER_WINDOW
)
259 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
261 else if ( evType
== wxEVT_LEAVE_WINDOW
)
263 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
266 else if ( evType
== wxEVT_MOUSEWHEEL
)
268 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
270 #endif // wxUSE_MOUSEWHEEL
271 else if ( evType
== wxEVT_CHAR
)
273 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
274 return !event
.GetSkipped();
280 // ----------------------------------------------------------------------------
281 // wxScrollHelper construction
282 // ----------------------------------------------------------------------------
284 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
286 wxASSERT_MSG( win
, _T("associated window can't be NULL in wxScrollHelper") );
288 m_xScrollPixelsPerLine
=
289 m_yScrollPixelsPerLine
=
294 m_xScrollLinesPerPage
=
295 m_yScrollLinesPerPage
= 0;
297 m_xScrollingEnabled
=
298 m_yScrollingEnabled
= true;
307 m_targetWindow
= (wxWindow
*)NULL
;
309 m_timerAutoScroll
= (wxTimer
*)NULL
;
315 m_win
->SetScrollHelper( this );
317 // by default, the associated window is also the target window
318 DoSetTargetWindow(win
);
321 wxScrollHelper::~wxScrollHelper()
328 // ----------------------------------------------------------------------------
329 // setting scrolling parameters
330 // ----------------------------------------------------------------------------
332 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
342 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
345 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
346 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
348 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
349 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
350 (xPos
!= m_xScrollPosition
) ||
351 (yPos
!= m_yScrollPosition
)
354 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
355 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
356 m_xScrollPosition
= xPos
;
357 m_yScrollPosition
= yPos
;
359 int w
= noUnitsX
* pixelsPerUnitX
;
360 int h
= noUnitsY
* pixelsPerUnitY
;
362 // For better backward compatibility we set persisting limits
363 // here not just the size. It makes SetScrollbars 'sticky'
364 // emulating the old non-autoscroll behaviour.
365 // m_targetWindow->SetVirtualSizeHints( w, h );
367 // The above should arguably be deprecated, this however we still need.
369 // take care not to set 0 virtual size, 0 means that we don't have any
370 // scrollbars and hence we should use the real size instead of the virtual
371 // one which is indicated by using wxDefaultCoord
372 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
373 h
? h
: wxDefaultCoord
);
375 if (do_refresh
&& !noRefresh
)
376 m_targetWindow
->Refresh(true, GetScrollRect());
378 #ifndef __WXUNIVERSAL__
379 // If the target is not the same as the window with the scrollbars,
380 // then we need to update the scrollbars here, since they won't have
381 // been updated by SetVirtualSize().
382 if ( m_targetWindow
!= m_win
)
383 #endif // !__WXUNIVERSAL__
387 #ifndef __WXUNIVERSAL__
390 // otherwise this has been done by AdjustScrollbars, above
392 #endif // !__WXUNIVERSAL__
395 // ----------------------------------------------------------------------------
396 // [target] window handling
397 // ----------------------------------------------------------------------------
399 void wxScrollHelper::DeleteEvtHandler()
401 // search for m_handler in the handler list
402 if ( m_win
&& m_handler
)
404 if ( m_win
->RemoveEventHandler(m_handler
) )
408 //else: something is very wrong, so better [maybe] leak memory than
409 // risk a crash because of double deletion
415 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
417 m_targetWindow
= target
;
419 target
->MacSetClipChildren( true ) ;
422 // install the event handler which will intercept the events we're
423 // interested in (but only do it for our real window, not the target window
424 // which we scroll - we don't need to hijack its events)
425 if ( m_targetWindow
== m_win
)
427 // if we already have a handler, delete it first
430 m_handler
= new wxScrollHelperEvtHandler(this);
431 m_targetWindow
->PushEventHandler(m_handler
);
435 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
437 wxCHECK_RET( target
, wxT("target window must not be NULL") );
439 if ( target
== m_targetWindow
)
442 DoSetTargetWindow(target
);
445 wxWindow
*wxScrollHelper::GetTargetWindow() const
447 return m_targetWindow
;
450 // ----------------------------------------------------------------------------
451 // scrolling implementation itself
452 // ----------------------------------------------------------------------------
454 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
456 int nScrollInc
= CalcScrollInc(event
);
457 if ( nScrollInc
== 0 )
459 // can't scroll further
465 int orient
= event
.GetOrientation();
466 if (orient
== wxHORIZONTAL
)
468 m_xScrollPosition
+= nScrollInc
;
469 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
473 m_yScrollPosition
+= nScrollInc
;
474 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
477 bool needsRefresh
= false;
480 if (orient
== wxHORIZONTAL
)
482 if ( m_xScrollingEnabled
)
484 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
493 if ( m_yScrollingEnabled
)
495 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
505 m_targetWindow
->Refresh(true, GetScrollRect());
509 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
513 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
515 int pos
= event
.GetPosition();
516 int orient
= event
.GetOrientation();
519 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
521 if (orient
== wxHORIZONTAL
)
522 nScrollInc
= - m_xScrollPosition
;
524 nScrollInc
= - m_yScrollPosition
;
526 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
528 if (orient
== wxHORIZONTAL
)
529 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
531 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
533 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
537 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
541 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
543 if (orient
== wxHORIZONTAL
)
544 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
546 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
548 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
550 if (orient
== wxHORIZONTAL
)
551 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
553 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
555 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
556 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
558 if (orient
== wxHORIZONTAL
)
559 nScrollInc
= pos
- m_xScrollPosition
;
561 nScrollInc
= pos
- m_yScrollPosition
;
564 if (orient
== wxHORIZONTAL
)
566 if (m_xScrollPixelsPerLine
> 0)
568 if ( m_xScrollPosition
+ nScrollInc
< 0 )
570 // As -ve as we can go
571 nScrollInc
= -m_xScrollPosition
;
573 else // check for the other bound
575 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
576 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
578 // As +ve as we can go
579 nScrollInc
= posMax
- m_xScrollPosition
;
584 m_targetWindow
->Refresh(true, GetScrollRect());
588 if ( m_yScrollPixelsPerLine
> 0 )
590 if ( m_yScrollPosition
+ nScrollInc
< 0 )
592 // As -ve as we can go
593 nScrollInc
= -m_yScrollPosition
;
595 else // check for the other bound
597 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
598 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
600 // As +ve as we can go
601 nScrollInc
= posMax
- m_yScrollPosition
;
607 // VZ: why do we do this? (FIXME)
608 m_targetWindow
->Refresh(true, GetScrollRect());
615 // Adjust the scrollbars - new version.
616 void wxScrollHelper::AdjustScrollbars()
618 static wxRecursionGuardFlag s_flagReentrancy
;
619 wxRecursionGuard
guard(s_flagReentrancy
);
620 if ( guard
.IsInside() )
622 // don't reenter AdjustScrollbars() while another call to
623 // AdjustScrollbars() is in progress because this may lead to calling
624 // ScrollWindow() twice and this can really happen under MSW if
625 // SetScrollbar() call below adds or removes the scrollbar which
626 // changes the window size and hence results in another
627 // AdjustScrollbars() call
634 int oldXScroll
= m_xScrollPosition
;
635 int oldYScroll
= m_yScrollPosition
;
637 // VZ: at least under Windows this loop is useless because when scrollbars
638 // [dis]appear we get a WM_SIZE resulting in another call to
639 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
640 // it here for now but it would be better to ensure that all ports
641 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
642 // necessary, and remove it later
643 // JACS: Stop potential infinite loop by limiting number of iterations
644 int iterationCount
= 0;
645 const int iterationMax
= 5;
650 GetTargetSize(&w
, 0);
652 // scroll lines per page: if 0, no scrolling is needed
655 if ( m_xScrollPixelsPerLine
== 0 )
657 // scrolling is disabled
659 m_xScrollPosition
= 0;
662 else // might need scrolling
664 // Round up integer division to catch any "leftover" client space.
665 const int wVirt
= m_targetWindow
->GetVirtualSize().GetWidth();
666 m_xScrollLines
= (wVirt
+ m_xScrollPixelsPerLine
- 1) / m_xScrollPixelsPerLine
;
668 // Calculate page size i.e. number of scroll units you get on the
669 // current client window.
670 linesPerPage
= w
/ m_xScrollPixelsPerLine
;
672 // Special case. When client and virtual size are very close but
673 // the client is big enough, kill scrollbar.
674 if ((linesPerPage
< m_xScrollLines
) && (w
>= wVirt
)) ++linesPerPage
;
676 if (linesPerPage
>= m_xScrollLines
)
678 // we're big enough to not need scrolling
681 m_xScrollPosition
= 0;
683 else // we do need a scrollbar
685 if ( linesPerPage
< 1 )
688 // Correct position if greater than extent of canvas minus
689 // the visible portion of it or if below zero
690 const int posMax
= m_xScrollLines
- linesPerPage
;
691 if ( m_xScrollPosition
> posMax
)
692 m_xScrollPosition
= posMax
;
693 else if ( m_xScrollPosition
< 0 )
694 m_xScrollPosition
= 0;
698 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
,
699 linesPerPage
, m_xScrollLines
);
701 // The amount by which we scroll when paging
702 SetScrollPageSize(wxHORIZONTAL
, linesPerPage
);
704 GetTargetSize(0, &h
);
706 if ( m_yScrollPixelsPerLine
== 0 )
708 // scrolling is disabled
710 m_yScrollPosition
= 0;
713 else // might need scrolling
715 // Round up integer division to catch any "leftover" client space.
716 const int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
717 m_yScrollLines
= ( hVirt
+ m_yScrollPixelsPerLine
- 1 ) / m_yScrollPixelsPerLine
;
719 // Calculate page size i.e. number of scroll units you get on the
720 // current client window.
721 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
723 // Special case. When client and virtual size are very close but
724 // the client is big enough, kill scrollbar.
725 if ((linesPerPage
< m_yScrollLines
) && (h
>= hVirt
)) ++linesPerPage
;
727 if (linesPerPage
>= m_yScrollLines
)
729 // we're big enough to not need scrolling
732 m_yScrollPosition
= 0;
734 else // we do need a scrollbar
736 if ( linesPerPage
< 1 )
739 // Correct position if greater than extent of canvas minus
740 // the visible portion of it or if below zero
741 const int posMax
= m_yScrollLines
- linesPerPage
;
742 if ( m_yScrollPosition
> posMax
)
743 m_yScrollPosition
= posMax
;
744 else if ( m_yScrollPosition
< 0 )
745 m_yScrollPosition
= 0;
749 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
750 linesPerPage
, m_yScrollLines
);
752 // The amount by which we scroll when paging
753 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
756 // If a scrollbar (dis)appeared as a result of this, adjust them again.
760 GetTargetSize( &w
, &h
);
761 } while ( (w
!= oldw
|| h
!= oldh
) && (iterationCount
< iterationMax
) );
764 // Sorry, some Motif-specific code to implement a backing pixmap
765 // for the wxRETAINED style. Implementing a backing store can't
766 // be entirely generic because it relies on the wxWindowDC implementation
767 // to duplicate X drawing calls for the backing pixmap.
769 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
771 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
773 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
774 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
775 if (m_targetWindow
->GetBackingPixmap() &&
776 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
777 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
779 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
780 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
783 if (!m_targetWindow
->GetBackingPixmap() &&
784 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
786 int depth
= wxDisplayDepth();
787 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
788 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
789 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
790 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
796 if (oldXScroll
!= m_xScrollPosition
)
798 if (m_xScrollingEnabled
)
799 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
802 m_targetWindow
->Refresh(true, GetScrollRect());
805 if (oldYScroll
!= m_yScrollPosition
)
807 if (m_yScrollingEnabled
)
808 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
811 m_targetWindow
->Refresh(true, GetScrollRect());
815 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
817 wxPoint pt
= dc
.GetDeviceOrigin();
819 // It may actually be correct to always query
820 // the m_sign from the DC here, but I leve the
821 // #ifdef GTK for now.
822 if (m_win
->GetLayoutDirection() == wxLayout_RightToLeft
)
823 dc
.SetDeviceOrigin( pt
.x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
,
824 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
827 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
828 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
829 dc
.SetUserScale( m_scaleX
, m_scaleY
);
832 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
834 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
835 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
837 m_xScrollPixelsPerLine
= xstep
;
838 m_yScrollPixelsPerLine
= ystep
;
840 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
841 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
843 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
844 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
845 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
850 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
853 *x_unit
= m_xScrollPixelsPerLine
;
855 *y_unit
= m_yScrollPixelsPerLine
;
859 int wxScrollHelper::GetScrollLines( int orient
) const
861 if ( orient
== wxHORIZONTAL
)
862 return m_xScrollLines
;
864 return m_yScrollLines
;
867 int wxScrollHelper::GetScrollPageSize(int orient
) const
869 if ( orient
== wxHORIZONTAL
)
870 return m_xScrollLinesPerPage
;
872 return m_yScrollLinesPerPage
;
875 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
877 if ( orient
== wxHORIZONTAL
)
878 m_xScrollLinesPerPage
= pageSize
;
880 m_yScrollLinesPerPage
= pageSize
;
884 * Scroll to given position (scroll position, not pixel position)
886 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
891 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
892 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
895 GetTargetSize(&w
, &h
);
897 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
899 int old_x
= m_xScrollPosition
;
900 m_xScrollPosition
= x_pos
;
902 // Calculate page size i.e. number of scroll units you get on the
903 // current client window
904 int noPagePositions
= w
/m_xScrollPixelsPerLine
;
905 if (noPagePositions
< 1) noPagePositions
= 1;
907 // Correct position if greater than extent of canvas minus
908 // the visible portion of it or if below zero
909 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
910 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
912 if (old_x
!= m_xScrollPosition
)
914 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
915 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
919 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
921 int old_y
= m_yScrollPosition
;
922 m_yScrollPosition
= y_pos
;
924 // Calculate page size i.e. number of scroll units you get on the
925 // current client window
926 int noPagePositions
= h
/m_yScrollPixelsPerLine
;
927 if (noPagePositions
< 1) noPagePositions
= 1;
929 // Correct position if greater than extent of canvas minus
930 // the visible portion of it or if below zero
931 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
932 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
934 if (old_y
!= m_yScrollPosition
)
936 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
937 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
943 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
945 m_xScrollingEnabled
= x_scroll
;
946 m_yScrollingEnabled
= y_scroll
;
949 // Where the current view starts from
950 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
953 *x
= m_xScrollPosition
;
955 *y
= m_yScrollPosition
;
958 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
961 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
963 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
966 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
969 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
971 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
974 // ----------------------------------------------------------------------------
976 // ----------------------------------------------------------------------------
978 bool wxScrollHelper::ScrollLayout()
980 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
982 // If we're the scroll target, take into account the
983 // virtual size and scrolled position of the window.
985 int x
= 0, y
= 0, w
= 0, h
= 0;
986 CalcScrolledPosition(0,0, &x
,&y
);
987 m_win
->GetVirtualSize(&w
, &h
);
988 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
992 // fall back to default for LayoutConstraints
993 return m_win
->wxWindow::Layout();
996 void wxScrollHelper::ScrollDoSetVirtualSize(int x
, int y
)
998 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
1001 if (m_win
->GetAutoLayout())
1005 // wxWindow's GetBestVirtualSize returns the actual window size,
1006 // whereas we want to return the virtual size
1007 wxSize
wxScrollHelper::ScrollGetBestVirtualSize() const
1009 wxSize
clientSize(m_win
->GetClientSize());
1010 if ( m_win
->GetSizer() )
1011 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
1016 // return the window best size from the given best virtual size
1018 wxScrollHelper::ScrollGetWindowSizeForVirtualSize(const wxSize
& size
) const
1020 // Only use the content to set the window size in the direction
1021 // where there's no scrolling; otherwise we're going to get a huge
1022 // window in the direction in which scrolling is enabled
1024 GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1026 wxSize minSize
= m_win
->GetMinSize();
1027 if ( !minSize
.IsFullySpecified() )
1028 minSize
= m_win
->GetSize();
1039 // ----------------------------------------------------------------------------
1041 // ----------------------------------------------------------------------------
1043 // Default OnSize resets scrollbars, if any
1044 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
1046 if ( m_targetWindow
->GetAutoLayout() )
1048 wxSize size
= m_targetWindow
->GetBestVirtualSize();
1050 // This will call ::Layout() and ::AdjustScrollbars()
1051 m_win
->SetVirtualSize( size
);
1059 // This calls OnDraw, having adjusted the origin according to the current
1061 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
1063 // don't use m_targetWindow here, this is always called for ourselves
1064 wxPaintDC
dc(m_win
);
1070 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1071 // compatibility here - if we used OnKeyDown(), the programs which process
1072 // arrows themselves in their OnChar() would never get the message and like
1073 // this they always have the priority
1074 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
1076 int stx
= 0, sty
= 0, // view origin
1077 szx
= 0, szy
= 0, // view size (total)
1078 clix
= 0, cliy
= 0; // view size (on screen)
1080 GetViewStart(&stx
, &sty
);
1081 GetTargetSize(&clix
, &cliy
);
1082 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
1084 if( m_xScrollPixelsPerLine
)
1086 clix
/= m_xScrollPixelsPerLine
;
1087 szx
/= m_xScrollPixelsPerLine
;
1094 if( m_yScrollPixelsPerLine
)
1096 cliy
/= m_yScrollPixelsPerLine
;
1097 szy
/= m_yScrollPixelsPerLine
;
1105 int xScrollOld
= m_xScrollPosition
,
1106 yScrollOld
= m_yScrollPosition
;
1109 switch ( event
.GetKeyCode() )
1112 dsty
= sty
- (5 * cliy
/ 6);
1113 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1117 Scroll(-1, sty
+ (5 * cliy
/ 6));
1121 Scroll(0, event
.ControlDown() ? 0 : -1);
1125 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1129 Scroll(-1, sty
- 1);
1133 Scroll(-1, sty
+ 1);
1137 Scroll(stx
- 1, -1);
1141 Scroll(stx
+ 1, -1);
1149 if ( m_xScrollPosition
!= xScrollOld
)
1151 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1153 event
.SetEventObject(m_win
);
1154 m_win
->GetEventHandler()->ProcessEvent(event
);
1157 if ( m_yScrollPosition
!= yScrollOld
)
1159 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1161 event
.SetEventObject(m_win
);
1162 m_win
->GetEventHandler()->ProcessEvent(event
);
1166 // ----------------------------------------------------------------------------
1167 // autoscroll stuff: these functions deal with sending fake scroll events when
1168 // a captured mouse is being held outside the window
1169 // ----------------------------------------------------------------------------
1171 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1173 // only send the event if the window is scrollable in this direction
1174 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1175 return win
->HasScrollbar(event
.GetOrientation());
1178 void wxScrollHelper::StopAutoScrolling()
1181 if ( m_timerAutoScroll
)
1183 delete m_timerAutoScroll
;
1184 m_timerAutoScroll
= (wxTimer
*)NULL
;
1189 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1191 StopAutoScrolling();
1196 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1198 // don't prevent the usual processing of the event from taking place
1201 // when a captured mouse leave a scrolled window we start generate
1202 // scrolling events to allow, for example, extending selection beyond the
1203 // visible area in some controls
1204 if ( wxWindow::GetCapture() == m_targetWindow
)
1206 // where is the mouse leaving?
1208 wxPoint pt
= event
.GetPosition();
1211 orient
= wxHORIZONTAL
;
1214 else if ( pt
.y
< 0 )
1216 orient
= wxVERTICAL
;
1219 else // we're lower or to the right of the window
1221 wxSize size
= m_targetWindow
->GetClientSize();
1222 if ( pt
.x
> size
.x
)
1224 orient
= wxHORIZONTAL
;
1225 pos
= m_xScrollLines
;
1227 else if ( pt
.y
> size
.y
)
1229 orient
= wxVERTICAL
;
1230 pos
= m_yScrollLines
;
1232 else // this should be impossible
1234 // but seems to happen sometimes under wxMSW - maybe it's a bug
1235 // there but for now just ignore it
1237 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1243 // only start the auto scroll timer if the window can be scrolled in
1245 if ( !m_targetWindow
->HasScrollbar(orient
) )
1249 delete m_timerAutoScroll
;
1250 m_timerAutoScroll
= new wxAutoScrollTimer
1252 m_targetWindow
, this,
1253 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1254 : wxEVT_SCROLLWIN_LINEDOWN
,
1258 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1265 #if wxUSE_MOUSEWHEEL
1267 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1269 m_wheelRotation
+= event
.GetWheelRotation();
1270 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1271 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1276 wxScrollWinEvent newEvent
;
1278 newEvent
.SetPosition(0);
1279 newEvent
.SetOrientation(wxVERTICAL
);
1280 newEvent
.SetEventObject(m_win
);
1282 if (event
.IsPageScroll())
1285 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1287 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1289 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1293 lines
*= event
.GetLinesPerAction();
1295 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1297 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1299 int times
= abs(lines
);
1300 for (; times
> 0; times
--)
1301 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1306 #endif // wxUSE_MOUSEWHEEL
1308 // ----------------------------------------------------------------------------
1309 // wxScrolledWindow implementation
1310 // ----------------------------------------------------------------------------
1312 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
1314 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
1315 EVT_PAINT(wxScrolledWindow::OnPaint
)
1318 bool wxScrolledWindow::Create(wxWindow
*parent
,
1323 const wxString
& name
)
1325 m_targetWindow
= this;
1327 MacSetClipChildren( true ) ;
1330 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
1335 wxScrolledWindow::~wxScrolledWindow()
1339 void wxScrolledWindow::OnPaint(wxPaintEvent
& event
)
1341 // the user code didn't really draw the window if we got here, so set this
1342 // flag to try to call OnDraw() later
1343 m_handler
->ResetDrawnFlag();
1349 WXLRESULT
wxScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1353 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1356 // we need to process arrows ourselves for scrolling
1357 if ( nMsg
== WM_GETDLGCODE
)
1359 rc
|= DLGC_WANTARROWS
;