1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/scrolwin.cpp
3 // Purpose: wxGenericScrolledWindow 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 #if !defined(__WXGTK__) || defined(__WXUNIVERSAL__)
35 #include "wx/dcclient.h"
37 #include "wx/scrolwin.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
62 IMPLEMENT_CLASS(wxScrolledWindow
, wxGenericScrolledWindow
)
66 style wxHSCROLL | wxVSCROLL
69 // ----------------------------------------------------------------------------
70 // wxScrollHelperEvtHandler: intercept the events from the window and forward
71 // them to wxScrollHelper
72 // ----------------------------------------------------------------------------
74 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
77 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
79 m_scrollHelper
= scrollHelper
;
82 virtual bool ProcessEvent(wxEvent
& event
);
84 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
87 wxScrollHelper
*m_scrollHelper
;
89 bool m_hasDrawnWindow
;
91 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
95 // ----------------------------------------------------------------------------
96 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
97 // a captured mouse is held outside the window
98 // ----------------------------------------------------------------------------
100 class wxAutoScrollTimer
: public wxTimer
103 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
104 wxEventType eventTypeToSend
,
105 int pos
, int orient
);
107 virtual void Notify();
111 wxScrollHelper
*m_scrollHelper
;
112 wxEventType m_eventType
;
116 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
119 // ============================================================================
121 // ============================================================================
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
128 wxScrollHelper
*scroll
,
129 wxEventType eventTypeToSend
,
133 m_scrollHelper
= scroll
;
134 m_eventType
= eventTypeToSend
;
139 void wxAutoScrollTimer::Notify()
141 // only do all this as long as the window is capturing the mouse
142 if ( wxWindow::GetCapture() != m_win
)
146 else // we still capture the mouse, continue generating events
148 // first scroll the window if we are allowed to do it
149 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
150 event1
.SetEventObject(m_win
);
151 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
152 m_win
->GetEventHandler()->ProcessEvent(event1
) )
154 // and then send a pseudo mouse-move event to refresh the selection
155 wxMouseEvent
event2(wxEVT_MOTION
);
156 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
158 // the mouse event coordinates should be client, not screen as
159 // returned by wxGetMousePosition
160 wxWindow
*parentTop
= m_win
;
161 while ( parentTop
->GetParent() )
162 parentTop
= parentTop
->GetParent();
163 wxPoint ptOrig
= parentTop
->GetPosition();
164 event2
.m_x
-= ptOrig
.x
;
165 event2
.m_y
-= ptOrig
.y
;
167 event2
.SetEventObject(m_win
);
169 // FIXME: we don't fill in the other members - ok?
171 m_win
->GetEventHandler()->ProcessEvent(event2
);
173 else // can't scroll further, stop
181 // ----------------------------------------------------------------------------
182 // wxScrollHelperEvtHandler
183 // ----------------------------------------------------------------------------
185 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
187 wxEventType evType
= event
.GetEventType();
189 // the explanation of wxEVT_PAINT processing hack: for historic reasons
190 // there are 2 ways to process this event in classes deriving from
191 // wxScrolledWindow. The user code may
193 // 1. override wxScrolledWindow::OnDraw(dc)
194 // 2. define its own OnPaint() handler
196 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
197 // always processes the draw event, so we can't just try the window
198 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
199 // (the latter would never be called in wxUniversal).
201 // So the solution is to have a flag telling us whether the user code drew
202 // anything in the window. We set it to true here but reset it to false in
203 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
204 // user code defined OnPaint() in the derived class)
205 m_hasDrawnWindow
= true;
207 // pass it on to the real handler
208 bool processed
= wxEvtHandler::ProcessEvent(event
);
210 // always process the size events ourselves, even if the user code handles
211 // them as well, as we need to AdjustScrollbars()
213 // NB: it is important to do it after processing the event in the normal
214 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
215 // scrollbar[s] (dis)appear and it should be seen by the user code
217 if ( evType
== wxEVT_SIZE
)
219 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
226 // normally, nothing more to do here - except if it was a paint event
227 // which wasn't really processed, then we'll try to call our
228 // OnDraw() below (from HandleOnPaint)
229 if ( m_hasDrawnWindow
)
235 // reset the skipped flag to false as it might have been set to true in
236 // ProcessEvent() above
239 if ( evType
== wxEVT_PAINT
)
241 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
245 if ( evType
== wxEVT_SCROLLWIN_TOP
||
246 evType
== wxEVT_SCROLLWIN_BOTTOM
||
247 evType
== wxEVT_SCROLLWIN_LINEUP
||
248 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
249 evType
== wxEVT_SCROLLWIN_PAGEUP
||
250 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
251 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
252 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
254 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
255 return !event
.GetSkipped();
258 if ( evType
== wxEVT_ENTER_WINDOW
)
260 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
262 else if ( evType
== wxEVT_LEAVE_WINDOW
)
264 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
267 else if ( evType
== wxEVT_MOUSEWHEEL
)
269 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
271 #endif // wxUSE_MOUSEWHEEL
272 else if ( evType
== wxEVT_CHAR
)
274 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
275 return !event
.GetSkipped();
281 // ----------------------------------------------------------------------------
282 // wxScrollHelper construction
283 // ----------------------------------------------------------------------------
285 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
287 m_xScrollPixelsPerLine
=
288 m_yScrollPixelsPerLine
=
293 m_xScrollLinesPerPage
=
294 m_yScrollLinesPerPage
= 0;
296 m_xScrollingEnabled
=
297 m_yScrollingEnabled
= true;
306 m_targetWindow
= (wxWindow
*)NULL
;
308 m_timerAutoScroll
= (wxTimer
*)NULL
;
316 wxScrollHelper::~wxScrollHelper()
323 // ----------------------------------------------------------------------------
324 // setting scrolling parameters
325 // ----------------------------------------------------------------------------
327 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
337 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
340 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
341 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
343 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
344 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
345 (xPos
!= m_xScrollPosition
) ||
346 (yPos
!= m_yScrollPosition
)
349 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
350 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
351 m_xScrollPosition
= xPos
;
352 m_yScrollPosition
= yPos
;
354 int w
= noUnitsX
* pixelsPerUnitX
;
355 int h
= noUnitsY
* pixelsPerUnitY
;
357 // For better backward compatibility we set persisting limits
358 // here not just the size. It makes SetScrollbars 'sticky'
359 // emulating the old non-autoscroll behaviour.
360 // m_targetWindow->SetVirtualSizeHints( w, h );
362 // The above should arguably be deprecated, this however we still need.
364 // take care not to set 0 virtual size, 0 means that we don't have any
365 // scrollbars and hence we should use the real size instead of the virtual
366 // one which is indicated by using wxDefaultCoord
367 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
368 h
? h
: wxDefaultCoord
);
370 if (do_refresh
&& !noRefresh
)
371 m_targetWindow
->Refresh(true, GetScrollRect());
373 #ifndef __WXUNIVERSAL__
374 // If the target is not the same as the window with the scrollbars,
375 // then we need to update the scrollbars here, since they won't have
376 // been updated by SetVirtualSize().
377 if ( m_targetWindow
!= m_win
)
378 #endif // !__WXUNIVERSAL__
382 #ifndef __WXUNIVERSAL__
385 // otherwise this has been done by AdjustScrollbars, above
387 #endif // !__WXUNIVERSAL__
390 // ----------------------------------------------------------------------------
391 // [target] window handling
392 // ----------------------------------------------------------------------------
394 void wxScrollHelper::DeleteEvtHandler()
396 // search for m_handler in the handler list
397 if ( m_win
&& m_handler
)
399 if ( m_win
->RemoveEventHandler(m_handler
) )
403 //else: something is very wrong, so better [maybe] leak memory than
404 // risk a crash because of double deletion
410 void wxScrollHelper::SetWindow(wxWindow
*win
)
412 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
416 // by default, the associated window is also the target window
417 DoSetTargetWindow(win
);
420 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
422 m_targetWindow
= target
;
424 target
->MacSetClipChildren( true ) ;
427 // install the event handler which will intercept the events we're
428 // interested in (but only do it for our real window, not the target window
429 // which we scroll - we don't need to hijack its events)
430 if ( m_targetWindow
== m_win
)
432 // if we already have a handler, delete it first
435 m_handler
= new wxScrollHelperEvtHandler(this);
436 m_targetWindow
->PushEventHandler(m_handler
);
440 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
442 wxCHECK_RET( target
, wxT("target window must not be NULL") );
444 if ( target
== m_targetWindow
)
447 DoSetTargetWindow(target
);
450 wxWindow
*wxScrollHelper::GetTargetWindow() const
452 return m_targetWindow
;
455 // ----------------------------------------------------------------------------
456 // scrolling implementation itself
457 // ----------------------------------------------------------------------------
459 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
461 int nScrollInc
= CalcScrollInc(event
);
462 if ( nScrollInc
== 0 )
464 // can't scroll further
470 int orient
= event
.GetOrientation();
471 if (orient
== wxHORIZONTAL
)
473 m_xScrollPosition
+= nScrollInc
;
474 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
478 m_yScrollPosition
+= nScrollInc
;
479 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
482 bool needsRefresh
= false;
485 if (orient
== wxHORIZONTAL
)
487 if ( m_xScrollingEnabled
)
489 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
498 if ( m_yScrollingEnabled
)
500 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
510 m_targetWindow
->Refresh(true, GetScrollRect());
514 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
518 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
520 int pos
= event
.GetPosition();
521 int orient
= event
.GetOrientation();
524 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
526 if (orient
== wxHORIZONTAL
)
527 nScrollInc
= - m_xScrollPosition
;
529 nScrollInc
= - m_yScrollPosition
;
531 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
533 if (orient
== wxHORIZONTAL
)
534 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
536 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
538 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
542 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
546 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
548 if (orient
== wxHORIZONTAL
)
549 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
551 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
553 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
555 if (orient
== wxHORIZONTAL
)
556 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
558 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
560 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
561 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
563 if (orient
== wxHORIZONTAL
)
564 nScrollInc
= pos
- m_xScrollPosition
;
566 nScrollInc
= pos
- m_yScrollPosition
;
569 if (orient
== wxHORIZONTAL
)
571 if (m_xScrollPixelsPerLine
> 0)
573 if ( m_xScrollPosition
+ nScrollInc
< 0 )
575 // As -ve as we can go
576 nScrollInc
= -m_xScrollPosition
;
578 else // check for the other bound
580 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
581 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
583 // As +ve as we can go
584 nScrollInc
= posMax
- m_xScrollPosition
;
589 m_targetWindow
->Refresh(true, GetScrollRect());
593 if ( m_yScrollPixelsPerLine
> 0 )
595 if ( m_yScrollPosition
+ nScrollInc
< 0 )
597 // As -ve as we can go
598 nScrollInc
= -m_yScrollPosition
;
600 else // check for the other bound
602 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
603 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
605 // As +ve as we can go
606 nScrollInc
= posMax
- m_yScrollPosition
;
612 // VZ: why do we do this? (FIXME)
613 m_targetWindow
->Refresh(true, GetScrollRect());
620 // Adjust the scrollbars - new version.
621 void wxScrollHelper::AdjustScrollbars()
623 static wxRecursionGuardFlag s_flagReentrancy
;
624 wxRecursionGuard
guard(s_flagReentrancy
);
625 if ( guard
.IsInside() )
627 // don't reenter AdjustScrollbars() while another call to
628 // AdjustScrollbars() is in progress because this may lead to calling
629 // ScrollWindow() twice and this can really happen under MSW if
630 // SetScrollbar() call below adds or removes the scrollbar which
631 // changes the window size and hence results in another
632 // AdjustScrollbars() call
639 int oldXScroll
= m_xScrollPosition
;
640 int oldYScroll
= m_yScrollPosition
;
642 // VZ: at least under Windows this loop is useless because when scrollbars
643 // [dis]appear we get a WM_SIZE resulting in another call to
644 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
645 // it here for now but it would be better to ensure that all ports
646 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
647 // necessary, and remove it later
648 // JACS: Stop potential infinite loop by limiting number of iterations
649 int iterationCount
= 0;
650 const int iterationMax
= 5;
655 GetTargetSize(&w
, 0);
657 // scroll lines per page: if 0, no scrolling is needed
660 if ( m_xScrollPixelsPerLine
== 0 )
662 // scrolling is disabled
664 m_xScrollPosition
= 0;
667 else // might need scrolling
669 // Round up integer division to catch any "leftover" client space.
670 const int wVirt
= m_targetWindow
->GetVirtualSize().GetWidth();
671 m_xScrollLines
= (wVirt
+ m_xScrollPixelsPerLine
- 1) / m_xScrollPixelsPerLine
;
673 // Calculate page size i.e. number of scroll units you get on the
674 // current client window.
675 linesPerPage
= w
/ m_xScrollPixelsPerLine
;
677 // Special case. When client and virtual size are very close but
678 // the client is big enough, kill scrollbar.
679 if ((linesPerPage
< m_xScrollLines
) && (w
>= wVirt
)) ++linesPerPage
;
681 if (linesPerPage
>= m_xScrollLines
)
683 // we're big enough to not need scrolling
686 m_xScrollPosition
= 0;
688 else // we do need a scrollbar
690 if ( linesPerPage
< 1 )
693 // Correct position if greater than extent of canvas minus
694 // the visible portion of it or if below zero
695 const int posMax
= m_xScrollLines
- linesPerPage
;
696 if ( m_xScrollPosition
> posMax
)
697 m_xScrollPosition
= posMax
;
698 else if ( m_xScrollPosition
< 0 )
699 m_xScrollPosition
= 0;
703 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
,
704 linesPerPage
, m_xScrollLines
);
706 // The amount by which we scroll when paging
707 SetScrollPageSize(wxHORIZONTAL
, linesPerPage
);
709 GetTargetSize(0, &h
);
711 if ( m_yScrollPixelsPerLine
== 0 )
713 // scrolling is disabled
715 m_yScrollPosition
= 0;
718 else // might need scrolling
720 // Round up integer division to catch any "leftover" client space.
721 const int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
722 m_yScrollLines
= ( hVirt
+ m_yScrollPixelsPerLine
- 1 ) / m_yScrollPixelsPerLine
;
724 // Calculate page size i.e. number of scroll units you get on the
725 // current client window.
726 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
728 // Special case. When client and virtual size are very close but
729 // the client is big enough, kill scrollbar.
730 if ((linesPerPage
< m_yScrollLines
) && (h
>= hVirt
)) ++linesPerPage
;
732 if (linesPerPage
>= m_yScrollLines
)
734 // we're big enough to not need scrolling
737 m_yScrollPosition
= 0;
739 else // we do need a scrollbar
741 if ( linesPerPage
< 1 )
744 // Correct position if greater than extent of canvas minus
745 // the visible portion of it or if below zero
746 const int posMax
= m_yScrollLines
- linesPerPage
;
747 if ( m_yScrollPosition
> posMax
)
748 m_yScrollPosition
= posMax
;
749 else if ( m_yScrollPosition
< 0 )
750 m_yScrollPosition
= 0;
754 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
755 linesPerPage
, m_yScrollLines
);
757 // The amount by which we scroll when paging
758 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
761 // If a scrollbar (dis)appeared as a result of this, adjust them again.
765 GetTargetSize( &w
, &h
);
766 } while ( (w
!= oldw
|| h
!= oldh
) && (iterationCount
< iterationMax
) );
769 // Sorry, some Motif-specific code to implement a backing pixmap
770 // for the wxRETAINED style. Implementing a backing store can't
771 // be entirely generic because it relies on the wxWindowDC implementation
772 // to duplicate X drawing calls for the backing pixmap.
774 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
776 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
778 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
779 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
780 if (m_targetWindow
->GetBackingPixmap() &&
781 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
782 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
784 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
785 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
788 if (!m_targetWindow
->GetBackingPixmap() &&
789 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
791 int depth
= wxDisplayDepth();
792 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
793 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
794 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
795 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
801 if (oldXScroll
!= m_xScrollPosition
)
803 if (m_xScrollingEnabled
)
804 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
807 m_targetWindow
->Refresh(true, GetScrollRect());
810 if (oldYScroll
!= m_yScrollPosition
)
812 if (m_yScrollingEnabled
)
813 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
816 m_targetWindow
->Refresh(true, GetScrollRect());
820 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
822 wxPoint pt
= dc
.GetDeviceOrigin();
823 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
824 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
825 dc
.SetUserScale( m_scaleX
, m_scaleY
);
828 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
830 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
831 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
833 m_xScrollPixelsPerLine
= xstep
;
834 m_yScrollPixelsPerLine
= ystep
;
836 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
837 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
839 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
840 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
841 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
846 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
849 *x_unit
= m_xScrollPixelsPerLine
;
851 *y_unit
= m_yScrollPixelsPerLine
;
854 int wxScrollHelper::GetScrollPageSize(int orient
) const
856 if ( orient
== wxHORIZONTAL
)
857 return m_xScrollLinesPerPage
;
859 return m_yScrollLinesPerPage
;
862 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
864 if ( orient
== wxHORIZONTAL
)
865 m_xScrollLinesPerPage
= pageSize
;
867 m_yScrollLinesPerPage
= pageSize
;
871 * Scroll to given position (scroll position, not pixel position)
873 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
878 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
879 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
882 GetTargetSize(&w
, &h
);
884 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
886 int old_x
= m_xScrollPosition
;
887 m_xScrollPosition
= x_pos
;
889 // Calculate page size i.e. number of scroll units you get on the
890 // current client window
891 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
892 if (noPagePositions
< 1) noPagePositions
= 1;
894 // Correct position if greater than extent of canvas minus
895 // the visible portion of it or if below zero
896 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
897 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
899 if (old_x
!= m_xScrollPosition
) {
900 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
901 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
905 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
907 int old_y
= m_yScrollPosition
;
908 m_yScrollPosition
= y_pos
;
910 // Calculate page size i.e. number of scroll units you get on the
911 // current client window
912 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
913 if (noPagePositions
< 1) noPagePositions
= 1;
915 // Correct position if greater than extent of canvas minus
916 // the visible portion of it or if below zero
917 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
918 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
920 if (old_y
!= m_yScrollPosition
) {
921 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
922 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
928 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
930 m_xScrollingEnabled
= x_scroll
;
931 m_yScrollingEnabled
= y_scroll
;
934 // Where the current view starts from
935 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
938 *x
= m_xScrollPosition
;
940 *y
= m_yScrollPosition
;
943 #if WXWIN_COMPATIBILITY_2_2
945 void wxScrollHelper::ViewStart(int *x
, int *y
) const
947 GetViewStart( x
, y
);
950 #endif // WXWIN_COMPATIBILITY_2_2
952 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
955 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
957 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
960 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
963 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
965 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
968 // ----------------------------------------------------------------------------
970 // ----------------------------------------------------------------------------
972 // Default OnSize resets scrollbars, if any
973 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
975 if ( m_targetWindow
->GetAutoLayout() )
977 wxSize size
= m_targetWindow
->GetBestVirtualSize();
979 // This will call ::Layout() and ::AdjustScrollbars()
980 m_win
->SetVirtualSize( size
);
988 // This calls OnDraw, having adjusted the origin according to the current
990 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
992 // don't use m_targetWindow here, this is always called for ourselves
999 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1000 // compatibility here - if we used OnKeyDown(), the programs which process
1001 // arrows themselves in their OnChar() would never get the message and like
1002 // this they always have the priority
1003 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
1005 int stx
, sty
, // view origin
1006 szx
, szy
, // view size (total)
1007 clix
, cliy
; // view size (on screen)
1009 GetViewStart(&stx
, &sty
);
1010 GetTargetSize(&clix
, &cliy
);
1011 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
1013 if( m_xScrollPixelsPerLine
)
1015 clix
/= m_xScrollPixelsPerLine
;
1016 szx
/= m_xScrollPixelsPerLine
;
1023 if( m_yScrollPixelsPerLine
)
1025 cliy
/= m_yScrollPixelsPerLine
;
1026 szy
/= m_yScrollPixelsPerLine
;
1034 int xScrollOld
= m_xScrollPosition
,
1035 yScrollOld
= m_yScrollPosition
;
1038 switch ( event
.GetKeyCode() )
1042 dsty
= sty
- (5 * cliy
/ 6);
1043 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1048 Scroll(-1, sty
+ (5 * cliy
/ 6));
1052 Scroll(0, event
.ControlDown() ? 0 : -1);
1056 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1060 Scroll(-1, sty
- 1);
1064 Scroll(-1, sty
+ 1);
1068 Scroll(stx
- 1, -1);
1072 Scroll(stx
+ 1, -1);
1080 if ( m_xScrollPosition
!= xScrollOld
)
1082 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1084 event
.SetEventObject(m_win
);
1085 m_win
->GetEventHandler()->ProcessEvent(event
);
1088 if ( m_yScrollPosition
!= yScrollOld
)
1090 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1092 event
.SetEventObject(m_win
);
1093 m_win
->GetEventHandler()->ProcessEvent(event
);
1097 // ----------------------------------------------------------------------------
1098 // autoscroll stuff: these functions deal with sending fake scroll events when
1099 // a captured mouse is being held outside the window
1100 // ----------------------------------------------------------------------------
1102 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1104 // only send the event if the window is scrollable in this direction
1105 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1106 return win
->HasScrollbar(event
.GetOrientation());
1109 void wxScrollHelper::StopAutoScrolling()
1112 if ( m_timerAutoScroll
)
1114 delete m_timerAutoScroll
;
1115 m_timerAutoScroll
= (wxTimer
*)NULL
;
1120 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1122 StopAutoScrolling();
1127 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1129 // don't prevent the usual processing of the event from taking place
1132 // when a captured mouse leave a scrolled window we start generate
1133 // scrolling events to allow, for example, extending selection beyond the
1134 // visible area in some controls
1135 if ( wxWindow::GetCapture() == m_targetWindow
)
1137 // where is the mouse leaving?
1139 wxPoint pt
= event
.GetPosition();
1142 orient
= wxHORIZONTAL
;
1145 else if ( pt
.y
< 0 )
1147 orient
= wxVERTICAL
;
1150 else // we're lower or to the right of the window
1152 wxSize size
= m_targetWindow
->GetClientSize();
1153 if ( pt
.x
> size
.x
)
1155 orient
= wxHORIZONTAL
;
1156 pos
= m_xScrollLines
;
1158 else if ( pt
.y
> size
.y
)
1160 orient
= wxVERTICAL
;
1161 pos
= m_yScrollLines
;
1163 else // this should be impossible
1165 // but seems to happen sometimes under wxMSW - maybe it's a bug
1166 // there but for now just ignore it
1168 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1174 // only start the auto scroll timer if the window can be scrolled in
1176 if ( !m_targetWindow
->HasScrollbar(orient
) )
1180 delete m_timerAutoScroll
;
1181 m_timerAutoScroll
= new wxAutoScrollTimer
1183 m_targetWindow
, this,
1184 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1185 : wxEVT_SCROLLWIN_LINEDOWN
,
1189 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1196 #if wxUSE_MOUSEWHEEL
1198 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1200 m_wheelRotation
+= event
.GetWheelRotation();
1201 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1202 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1207 wxScrollWinEvent newEvent
;
1209 newEvent
.SetPosition(0);
1210 newEvent
.SetOrientation(wxVERTICAL
);
1211 newEvent
.SetEventObject(m_win
);
1213 if (event
.IsPageScroll())
1216 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1218 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1220 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1224 lines
*= event
.GetLinesPerAction();
1226 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1228 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1230 int times
= abs(lines
);
1231 for (; times
> 0; times
--)
1232 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1237 #endif // wxUSE_MOUSEWHEEL
1239 // ----------------------------------------------------------------------------
1240 // wxGenericScrolledWindow implementation
1241 // ----------------------------------------------------------------------------
1243 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1245 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1246 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1249 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1254 const wxString
& name
)
1256 m_targetWindow
= this;
1258 MacSetClipChildren( true ) ;
1261 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
1266 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1270 bool wxGenericScrolledWindow::Layout()
1272 if (GetSizer() && m_targetWindow
== this)
1274 // If we're the scroll target, take into account the
1275 // virtual size and scrolled position of the window.
1278 CalcScrolledPosition(0,0, &x
,&y
);
1279 GetVirtualSize(&w
, &h
);
1280 GetSizer()->SetDimension(x
, y
, w
, h
);
1284 // fall back to default for LayoutConstraints
1285 return wxPanel::Layout();
1288 void wxGenericScrolledWindow::DoSetVirtualSize(int x
, int y
)
1290 wxPanel::DoSetVirtualSize( x
, y
);
1293 if (GetAutoLayout())
1297 // wxWindow's GetBestVirtualSize returns the actual window size,
1298 // whereas we want to return the virtual size
1299 wxSize
wxGenericScrolledWindow::GetBestVirtualSize() const
1301 wxSize
clientSize( GetClientSize() );
1304 wxSize
minSize( GetSizer()->CalcMin() );
1306 return wxSize( wxMax( clientSize
.x
, minSize
.x
), wxMax( clientSize
.y
, minSize
.y
) );
1312 // return the size best suited for the current window
1313 // (this isn't a virtual size, this is a sensible size for the window)
1314 wxSize
wxGenericScrolledWindow::DoGetBestSize() const
1320 wxSize b
= GetSizer()->GetMinSize();
1322 // Only use the content to set the window size in the direction
1323 // where there's no scrolling; otherwise we're going to get a huge
1324 // window in the direction in which scrolling is enabled
1326 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1329 if ( GetMinSize().IsFullySpecified() )
1330 minSize
= GetMinSize();
1332 minSize
= GetSize();
1341 return wxWindow::DoGetBestSize();
1343 // Add any difference between size and client size
1344 wxSize diff
= GetSize() - GetClientSize();
1345 best
.x
+= wxMax(0, diff
.x
);
1346 best
.y
+= wxMax(0, diff
.y
);
1351 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1353 // the user code didn't really draw the window if we got here, so set this
1354 // flag to try to call OnDraw() later
1355 m_handler
->ResetDrawnFlag();
1362 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1366 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1369 // we need to process arrows ourselves for scrolling
1370 if ( nMsg
== WM_GETDLGCODE
)
1372 rc
|= DLGC_WANTARROWS
;