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 // ----------------------------------------------------------------------------
21 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
22 #pragma implementation "genscrolwin.h"
26 #define XtDisplay XTDISPLAY
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
36 #if !defined(__WXGTK__) || defined(__WXUNIVERSAL__)
39 #include "wx/dcclient.h"
41 #include "wx/scrolwin.h"
47 #include "wx/recguard.h"
50 #include <windows.h> // for DLGC_WANTARROWS
54 // For wxRETAINED implementation
55 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
56 //This code switches off the compiler warnings
57 # pragma message disable nosimpint
61 # pragma message enable nosimpint
65 IMPLEMENT_CLASS(wxScrolledWindow
, wxGenericScrolledWindow
)
69 style wxHSCROLL | wxVSCROLL
72 // ----------------------------------------------------------------------------
73 // wxScrollHelperEvtHandler: intercept the events from the window and forward
74 // them to wxScrollHelper
75 // ----------------------------------------------------------------------------
77 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
80 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
82 m_scrollHelper
= scrollHelper
;
85 virtual bool ProcessEvent(wxEvent
& event
);
87 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
90 wxScrollHelper
*m_scrollHelper
;
92 bool m_hasDrawnWindow
;
94 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
98 // ----------------------------------------------------------------------------
99 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
100 // a captured mouse is held outside the window
101 // ----------------------------------------------------------------------------
103 class wxAutoScrollTimer
: public wxTimer
106 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
107 wxEventType eventTypeToSend
,
108 int pos
, int orient
);
110 virtual void Notify();
114 wxScrollHelper
*m_scrollHelper
;
115 wxEventType m_eventType
;
119 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
122 // ============================================================================
124 // ============================================================================
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
131 wxScrollHelper
*scroll
,
132 wxEventType eventTypeToSend
,
136 m_scrollHelper
= scroll
;
137 m_eventType
= eventTypeToSend
;
142 void wxAutoScrollTimer::Notify()
144 // only do all this as long as the window is capturing the mouse
145 if ( wxWindow::GetCapture() != m_win
)
149 else // we still capture the mouse, continue generating events
151 // first scroll the window if we are allowed to do it
152 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
153 event1
.SetEventObject(m_win
);
154 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
155 m_win
->GetEventHandler()->ProcessEvent(event1
) )
157 // and then send a pseudo mouse-move event to refresh the selection
158 wxMouseEvent
event2(wxEVT_MOTION
);
159 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
161 // the mouse event coordinates should be client, not screen as
162 // returned by wxGetMousePosition
163 wxWindow
*parentTop
= m_win
;
164 while ( parentTop
->GetParent() )
165 parentTop
= parentTop
->GetParent();
166 wxPoint ptOrig
= parentTop
->GetPosition();
167 event2
.m_x
-= ptOrig
.x
;
168 event2
.m_y
-= ptOrig
.y
;
170 event2
.SetEventObject(m_win
);
172 // FIXME: we don't fill in the other members - ok?
174 m_win
->GetEventHandler()->ProcessEvent(event2
);
176 else // can't scroll further, stop
184 // ----------------------------------------------------------------------------
185 // wxScrollHelperEvtHandler
186 // ----------------------------------------------------------------------------
188 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
190 wxEventType evType
= event
.GetEventType();
192 // the explanation of wxEVT_PAINT processing hack: for historic reasons
193 // there are 2 ways to process this event in classes deriving from
194 // wxScrolledWindow. The user code may
196 // 1. override wxScrolledWindow::OnDraw(dc)
197 // 2. define its own OnPaint() handler
199 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
200 // always processes the draw event, so we can't just try the window
201 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
202 // (the latter would never be called in wxUniversal).
204 // So the solution is to have a flag telling us whether the user code drew
205 // anything in the window. We set it to true here but reset it to false in
206 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
207 // user code defined OnPaint() in the derived class)
208 m_hasDrawnWindow
= true;
210 // pass it on to the real handler
211 bool processed
= wxEvtHandler::ProcessEvent(event
);
213 // always process the size events ourselves, even if the user code handles
214 // them as well, as we need to AdjustScrollbars()
216 // NB: it is important to do it after processing the event in the normal
217 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
218 // scrollbar[s] (dis)appear and it should be seen by the user code
220 if ( evType
== wxEVT_SIZE
)
222 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
229 // normally, nothing more to do here - except if it was a paint event
230 // which wasn't really processed, then we'll try to call our
231 // OnDraw() below (from HandleOnPaint)
232 if ( m_hasDrawnWindow
)
238 // reset the skipped flag to false as it might have been set to true in
239 // ProcessEvent() above
242 if ( evType
== wxEVT_PAINT
)
244 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
248 if ( evType
== wxEVT_SCROLLWIN_TOP
||
249 evType
== wxEVT_SCROLLWIN_BOTTOM
||
250 evType
== wxEVT_SCROLLWIN_LINEUP
||
251 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
252 evType
== wxEVT_SCROLLWIN_PAGEUP
||
253 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
254 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
255 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
257 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
258 return !event
.GetSkipped();
261 if ( evType
== wxEVT_ENTER_WINDOW
)
263 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
265 else if ( evType
== wxEVT_LEAVE_WINDOW
)
267 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
270 else if ( evType
== wxEVT_MOUSEWHEEL
)
272 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
274 #endif // wxUSE_MOUSEWHEEL
275 else if ( evType
== wxEVT_CHAR
)
277 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
278 return !event
.GetSkipped();
284 // ----------------------------------------------------------------------------
285 // wxScrollHelper construction
286 // ----------------------------------------------------------------------------
288 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
290 m_xScrollPixelsPerLine
=
291 m_yScrollPixelsPerLine
=
296 m_xScrollLinesPerPage
=
297 m_yScrollLinesPerPage
= 0;
299 m_xScrollingEnabled
=
300 m_yScrollingEnabled
= true;
309 m_targetWindow
= (wxWindow
*)NULL
;
311 m_timerAutoScroll
= (wxTimer
*)NULL
;
319 wxScrollHelper::~wxScrollHelper()
326 // ----------------------------------------------------------------------------
327 // setting scrolling parameters
328 // ----------------------------------------------------------------------------
330 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
340 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
343 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
344 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
346 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
347 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
348 (xPos
!= m_xScrollPosition
) ||
349 (yPos
!= m_yScrollPosition
)
352 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
353 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
354 m_xScrollPosition
= xPos
;
355 m_yScrollPosition
= yPos
;
357 int w
= noUnitsX
* pixelsPerUnitX
;
358 int h
= noUnitsY
* pixelsPerUnitY
;
360 // For better backward compatibility we set persisting limits
361 // here not just the size. It makes SetScrollbars 'sticky'
362 // emulating the old non-autoscroll behaviour.
364 m_targetWindow
->SetVirtualSizeHints( w
, h
);
366 // The above should arguably be deprecated, this however we still need.
368 m_targetWindow
->SetVirtualSize( w
, h
);
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 // install the event handler which will intercept the events we're
425 // interested in (but only do it for our real window, not the target window
426 // which we scroll - we don't need to hijack its events)
427 if ( m_targetWindow
== m_win
)
429 // if we already have a handler, delete it first
432 m_handler
= new wxScrollHelperEvtHandler(this);
433 m_targetWindow
->PushEventHandler(m_handler
);
437 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
439 wxCHECK_RET( target
, wxT("target window must not be NULL") );
441 if ( target
== m_targetWindow
)
444 DoSetTargetWindow(target
);
447 wxWindow
*wxScrollHelper::GetTargetWindow() const
449 return m_targetWindow
;
452 // ----------------------------------------------------------------------------
453 // scrolling implementation itself
454 // ----------------------------------------------------------------------------
456 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
458 int nScrollInc
= CalcScrollInc(event
);
459 if ( nScrollInc
== 0 )
461 // can't scroll further
467 int orient
= event
.GetOrientation();
468 if (orient
== wxHORIZONTAL
)
470 m_xScrollPosition
+= nScrollInc
;
471 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
475 m_yScrollPosition
+= nScrollInc
;
476 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
479 bool needsRefresh
= false;
482 if (orient
== wxHORIZONTAL
)
484 if ( m_xScrollingEnabled
)
486 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
495 if ( m_yScrollingEnabled
)
497 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
507 m_targetWindow
->Refresh(true, GetScrollRect());
511 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
515 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
517 int pos
= event
.GetPosition();
518 int orient
= event
.GetOrientation();
521 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
523 if (orient
== wxHORIZONTAL
)
524 nScrollInc
= - m_xScrollPosition
;
526 nScrollInc
= - m_yScrollPosition
;
528 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
530 if (orient
== wxHORIZONTAL
)
531 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
533 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
535 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
539 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
543 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
545 if (orient
== wxHORIZONTAL
)
546 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
548 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
550 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
552 if (orient
== wxHORIZONTAL
)
553 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
555 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
557 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
558 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
560 if (orient
== wxHORIZONTAL
)
561 nScrollInc
= pos
- m_xScrollPosition
;
563 nScrollInc
= pos
- m_yScrollPosition
;
566 if (orient
== wxHORIZONTAL
)
568 if (m_xScrollPixelsPerLine
> 0)
571 GetTargetSize(&w
, &h
);
573 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
574 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
578 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
579 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
580 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
581 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
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
645 GetTargetSize(&w
, 0);
647 if (m_xScrollPixelsPerLine
== 0)
650 m_xScrollPosition
= 0;
651 m_win
->SetScrollbar (wxHORIZONTAL
, 0, 0, 0, false);
655 m_xScrollLines
= m_targetWindow
->GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine
;
657 // Calculate page size i.e. number of scroll units you get on the
658 // current client window
659 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
660 if (noPagePositions
< 1) noPagePositions
= 1;
661 if ( noPagePositions
> m_xScrollLines
)
662 noPagePositions
= m_xScrollLines
;
664 // Correct position if greater than extent of canvas minus
665 // the visible portion of it or if below zero
666 m_xScrollPosition
= wxMin( m_xScrollLines
- noPagePositions
, m_xScrollPosition
);
667 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
669 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
670 // The amount by which we scroll when paging
671 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
674 GetTargetSize(0, &h
);
676 // scroll lines per page: if 0, no scrolling is needed
679 if ( m_yScrollPixelsPerLine
== 0 )
681 // scrolling is disabled
683 m_yScrollPosition
= 0;
686 else // might need scrolling
688 int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
689 m_yScrollLines
= hVirt
/ m_yScrollPixelsPerLine
;
691 // Calculate page size i.e. number of scroll units you get on the
692 // current client window
693 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
694 if ( linesPerPage
>= m_yScrollLines
)
696 // we're big enough to not need scrolling
699 m_yScrollPosition
= 0;
701 else // we do need a scrollbar
703 if ( linesPerPage
< 1 )
706 // Correct position if greater than extent of canvas minus
707 // the visible portion of it or if below zero
708 const int posMax
= m_yScrollLines
- linesPerPage
;
709 if ( m_yScrollPosition
> posMax
)
710 m_yScrollPosition
= posMax
;
711 else if ( m_yScrollPosition
< 0 )
712 m_yScrollPosition
= 0;
716 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
717 linesPerPage
, m_yScrollLines
);
719 // The amount by which we scroll when paging
720 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
723 // If a scrollbar (dis)appeared as a result of this, adjust them again.
727 GetTargetSize( &w
, &h
);
728 } while ( w
!= oldw
|| h
!= oldh
);
731 // Sorry, some Motif-specific code to implement a backing pixmap
732 // for the wxRETAINED style. Implementing a backing store can't
733 // be entirely generic because it relies on the wxWindowDC implementation
734 // to duplicate X drawing calls for the backing pixmap.
736 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
738 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
740 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
741 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
742 if (m_targetWindow
->GetBackingPixmap() &&
743 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
744 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
746 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
747 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
750 if (!m_targetWindow
->GetBackingPixmap() &&
751 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
753 int depth
= wxDisplayDepth();
754 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
755 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
756 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
757 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
763 if (oldXScroll
!= m_xScrollPosition
)
765 if (m_xScrollingEnabled
)
766 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
769 m_targetWindow
->Refresh(true, GetScrollRect());
772 if (oldYScroll
!= m_yScrollPosition
)
774 if (m_yScrollingEnabled
)
775 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
778 m_targetWindow
->Refresh(true, GetScrollRect());
782 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
784 wxPoint pt
= dc
.GetDeviceOrigin();
785 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
786 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
787 dc
.SetUserScale( m_scaleX
, m_scaleY
);
790 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
792 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
793 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
795 m_xScrollPixelsPerLine
= xstep
;
796 m_yScrollPixelsPerLine
= ystep
;
798 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
799 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
801 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
802 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
803 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
808 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
811 *x_unit
= m_xScrollPixelsPerLine
;
813 *y_unit
= m_yScrollPixelsPerLine
;
816 int wxScrollHelper::GetScrollPageSize(int orient
) const
818 if ( orient
== wxHORIZONTAL
)
819 return m_xScrollLinesPerPage
;
821 return m_yScrollLinesPerPage
;
824 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
826 if ( orient
== wxHORIZONTAL
)
827 m_xScrollLinesPerPage
= pageSize
;
829 m_yScrollLinesPerPage
= pageSize
;
833 * Scroll to given position (scroll position, not pixel position)
835 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
840 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
841 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
844 GetTargetSize(&w
, &h
);
846 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
848 int old_x
= m_xScrollPosition
;
849 m_xScrollPosition
= x_pos
;
851 // Calculate page size i.e. number of scroll units you get on the
852 // current client window
853 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
854 if (noPagePositions
< 1) noPagePositions
= 1;
856 // Correct position if greater than extent of canvas minus
857 // the visible portion of it or if below zero
858 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
859 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
861 if (old_x
!= m_xScrollPosition
) {
862 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
863 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
867 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
869 int old_y
= m_yScrollPosition
;
870 m_yScrollPosition
= y_pos
;
872 // Calculate page size i.e. number of scroll units you get on the
873 // current client window
874 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
875 if (noPagePositions
< 1) noPagePositions
= 1;
877 // Correct position if greater than extent of canvas minus
878 // the visible portion of it or if below zero
879 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
880 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
882 if (old_y
!= m_yScrollPosition
) {
883 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
884 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
890 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
892 m_xScrollingEnabled
= x_scroll
;
893 m_yScrollingEnabled
= y_scroll
;
896 // Where the current view starts from
897 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
900 *x
= m_xScrollPosition
;
902 *y
= m_yScrollPosition
;
905 #if WXWIN_COMPATIBILITY_2_2
907 void wxScrollHelper::ViewStart(int *x
, int *y
) const
909 GetViewStart( x
, y
);
912 #endif // WXWIN_COMPATIBILITY_2_2
914 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
917 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
919 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
922 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
925 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
927 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
930 // ----------------------------------------------------------------------------
932 // ----------------------------------------------------------------------------
934 // Default OnSize resets scrollbars, if any
935 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
937 if( m_win
->GetAutoLayout() || m_targetWindow
->GetAutoLayout() )
939 if ( m_targetWindow
!= m_win
)
940 m_targetWindow
->FitInside();
944 // FIXME: Something is really weird here... This should be
945 // called by FitInside above (and apparently is), yet the
946 // scrollsub sample will get the scrollbar wrong if resized
947 // quickly. This masks the bug, but is surely not the right
955 // This calls OnDraw, having adjusted the origin according to the current
957 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
959 // don't use m_targetWindow here, this is always called for ourselves
966 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
967 // compatibility here - if we used OnKeyDown(), the programs which process
968 // arrows themselves in their OnChar() would never get the message and like
969 // this they always have the priority
970 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
972 int stx
, sty
, // view origin
973 szx
, szy
, // view size (total)
974 clix
, cliy
; // view size (on screen)
976 GetViewStart(&stx
, &sty
);
977 GetTargetSize(&clix
, &cliy
);
978 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
980 if( m_xScrollPixelsPerLine
)
982 clix
/= m_xScrollPixelsPerLine
;
983 szx
/= m_xScrollPixelsPerLine
;
990 if( m_yScrollPixelsPerLine
)
992 cliy
/= m_yScrollPixelsPerLine
;
993 szy
/= m_yScrollPixelsPerLine
;
1001 int xScrollOld
= m_xScrollPosition
,
1002 yScrollOld
= m_yScrollPosition
;
1005 switch ( event
.GetKeyCode() )
1009 dsty
= sty
- (5 * cliy
/ 6);
1010 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1015 Scroll(-1, sty
+ (5 * cliy
/ 6));
1019 Scroll(0, event
.ControlDown() ? 0 : -1);
1023 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1027 Scroll(-1, sty
- 1);
1031 Scroll(-1, sty
+ 1);
1035 Scroll(stx
- 1, -1);
1039 Scroll(stx
+ 1, -1);
1047 if ( m_xScrollPosition
!= xScrollOld
)
1049 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1051 event
.SetEventObject(m_win
);
1052 m_win
->GetEventHandler()->ProcessEvent(event
);
1055 if ( m_yScrollPosition
!= yScrollOld
)
1057 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1059 event
.SetEventObject(m_win
);
1060 m_win
->GetEventHandler()->ProcessEvent(event
);
1064 // ----------------------------------------------------------------------------
1065 // autoscroll stuff: these functions deal with sending fake scroll events when
1066 // a captured mouse is being held outside the window
1067 // ----------------------------------------------------------------------------
1069 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1071 // only send the event if the window is scrollable in this direction
1072 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1073 return win
->HasScrollbar(event
.GetOrientation());
1076 void wxScrollHelper::StopAutoScrolling()
1079 if ( m_timerAutoScroll
)
1081 delete m_timerAutoScroll
;
1082 m_timerAutoScroll
= (wxTimer
*)NULL
;
1087 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1089 StopAutoScrolling();
1094 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1096 // don't prevent the usual processing of the event from taking place
1099 // when a captured mouse leave a scrolled window we start generate
1100 // scrolling events to allow, for example, extending selection beyond the
1101 // visible area in some controls
1102 if ( wxWindow::GetCapture() == m_targetWindow
)
1104 // where is the mouse leaving?
1106 wxPoint pt
= event
.GetPosition();
1109 orient
= wxHORIZONTAL
;
1112 else if ( pt
.y
< 0 )
1114 orient
= wxVERTICAL
;
1117 else // we're lower or to the right of the window
1119 wxSize size
= m_targetWindow
->GetClientSize();
1120 if ( pt
.x
> size
.x
)
1122 orient
= wxHORIZONTAL
;
1123 pos
= m_xScrollLines
;
1125 else if ( pt
.y
> size
.y
)
1127 orient
= wxVERTICAL
;
1128 pos
= m_yScrollLines
;
1130 else // this should be impossible
1132 // but seems to happen sometimes under wxMSW - maybe it's a bug
1133 // there but for now just ignore it
1135 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1141 // only start the auto scroll timer if the window can be scrolled in
1143 if ( !m_targetWindow
->HasScrollbar(orient
) )
1147 delete m_timerAutoScroll
;
1148 m_timerAutoScroll
= new wxAutoScrollTimer
1150 m_targetWindow
, this,
1151 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1152 : wxEVT_SCROLLWIN_LINEDOWN
,
1156 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1163 #if wxUSE_MOUSEWHEEL
1165 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1167 m_wheelRotation
+= event
.GetWheelRotation();
1168 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1169 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1174 wxScrollWinEvent newEvent
;
1176 newEvent
.SetPosition(0);
1177 newEvent
.SetOrientation(wxVERTICAL
);
1178 newEvent
.SetEventObject(m_win
);
1180 if (event
.IsPageScroll())
1183 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1185 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1187 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1191 lines
*= event
.GetLinesPerAction();
1193 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1195 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1197 int times
= abs(lines
);
1198 for (; times
> 0; times
--)
1199 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1204 #endif // wxUSE_MOUSEWHEEL
1206 // ----------------------------------------------------------------------------
1207 // wxGenericScrolledWindow implementation
1208 // ----------------------------------------------------------------------------
1210 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1212 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1213 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1216 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1221 const wxString
& name
)
1223 m_targetWindow
= this;
1225 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
1230 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1234 bool wxGenericScrolledWindow::Layout()
1236 if (GetSizer() && m_targetWindow
== this)
1238 // If we're the scroll target, take into account the
1239 // virtual size and scrolled position of the window.
1242 CalcScrolledPosition(0,0, &x
,&y
);
1243 GetVirtualSize(&w
, &h
);
1244 GetSizer()->SetDimension(x
, y
, w
, h
);
1248 // fall back to default for LayoutConstraints
1249 return wxPanel::Layout();
1252 void wxGenericScrolledWindow::DoSetVirtualSize(int x
, int y
)
1254 wxPanel::DoSetVirtualSize( x
, y
);
1257 if (GetAutoLayout())
1261 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1263 // the user code didn't really draw the window if we got here, so set this
1264 // flag to try to call OnDraw() later
1265 m_handler
->ResetDrawnFlag();
1272 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1276 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1279 // we need to process arrows ourselves for scrolling
1280 if ( nMsg
== WM_GETDLGCODE
)
1282 rc
|= DLGC_WANTARROWS
;