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) wxWindows team
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
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 <windows.h> // for DLGC_WANTARROWS
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
)
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
179 // ----------------------------------------------------------------------------
180 // wxScrollHelperEvtHandler
181 // ----------------------------------------------------------------------------
183 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
185 wxEventType evType
= event
.GetEventType();
187 // the explanation of wxEVT_PAINT processing hack: for historic reasons
188 // there are 2 ways to process this event in classes deriving from
189 // wxScrolledWindow. The user code may
191 // 1. override wxScrolledWindow::OnDraw(dc)
192 // 2. define its own OnPaint() handler
194 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
195 // always processes the draw event, so we can't just try the window
196 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
197 // (the latter would never be called in wxUniversal).
199 // So the solution is to have a flag telling us whether the user code drew
200 // anything in the window. We set it to true here but reset it to false in
201 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
202 // user code defined OnPaint() in the derived class)
203 m_hasDrawnWindow
= TRUE
;
205 // pass it on to the real handler
206 bool processed
= wxEvtHandler::ProcessEvent(event
);
208 // always process the size events ourselves, even if the user code handles
209 // them as well, as we need to AdjustScrollbars()
211 // NB: it is important to do it after processing the event in the normal
212 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
213 // scrollbar[s] (dis)appear and it should be seen by the user code
215 if ( evType
== wxEVT_SIZE
)
217 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
224 // normally, nothing more to do here - except if it was a paint event
225 // which wasn't really processed, then we'll try to call our
226 // OnDraw() below (from HandleOnPaint)
227 if ( m_hasDrawnWindow
)
233 // reset the skipped flag to FALSE as it might have been set to TRUE in
234 // ProcessEvent() above
237 if ( evType
== wxEVT_PAINT
)
239 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
243 if ( evType
== wxEVT_SCROLLWIN_TOP
||
244 evType
== wxEVT_SCROLLWIN_BOTTOM
||
245 evType
== wxEVT_SCROLLWIN_LINEUP
||
246 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
247 evType
== wxEVT_SCROLLWIN_PAGEUP
||
248 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
249 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
250 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
252 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
253 return !event
.GetSkipped();
256 if ( evType
== wxEVT_ENTER_WINDOW
)
258 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
260 else if ( evType
== wxEVT_LEAVE_WINDOW
)
262 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
265 else if ( evType
== wxEVT_MOUSEWHEEL
)
267 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
269 #endif // wxUSE_MOUSEWHEEL
270 else if ( evType
== wxEVT_CHAR
)
272 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
273 return !event
.GetSkipped();
279 // ----------------------------------------------------------------------------
280 // wxScrollHelper construction
281 // ----------------------------------------------------------------------------
283 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
285 m_xScrollPixelsPerLine
=
286 m_yScrollPixelsPerLine
=
291 m_xScrollLinesPerPage
=
292 m_yScrollLinesPerPage
= 0;
294 m_xScrollingEnabled
=
295 m_yScrollingEnabled
= TRUE
;
304 m_targetWindow
= (wxWindow
*)NULL
;
306 m_timerAutoScroll
= (wxTimer
*)NULL
;
314 wxScrollHelper::~wxScrollHelper()
321 // ----------------------------------------------------------------------------
322 // setting scrolling parameters
323 // ----------------------------------------------------------------------------
325 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
335 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
338 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
339 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
341 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
342 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
343 (xPos
!= m_xScrollPosition
) ||
344 (yPos
!= m_yScrollPosition
)
347 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
348 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
349 m_xScrollPosition
= xPos
;
350 m_yScrollPosition
= yPos
;
352 int w
= noUnitsX
* pixelsPerUnitX
;
353 int h
= noUnitsY
* pixelsPerUnitY
;
355 // For better backward compatibility we set persisting limits
356 // here not just the size. It makes SetScrollbars 'sticky'
357 // emulating the old non-autoscroll behaviour.
359 m_targetWindow
->SetVirtualSizeHints( w
, h
);
361 // The above should arguably be deprecated, this however we still need.
363 m_targetWindow
->SetVirtualSize( w
, h
);
365 if (do_refresh
&& !noRefresh
)
366 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
368 #ifndef __WXUNIVERSAL__
369 // If the target is not the same as the window with the scrollbars,
370 // then we need to update the scrollbars here, since they won't have
371 // been updated by SetVirtualSize().
372 if ( m_targetWindow
!= m_win
)
373 #endif // !__WXUNIVERSAL__
377 #ifndef __WXUNIVERSAL__
380 // otherwise this has been done by AdjustScrollbars, above
382 m_targetWindow
->Update() ;
385 #endif // !__WXUNIVERSAL__
388 // ----------------------------------------------------------------------------
389 // [target] window handling
390 // ----------------------------------------------------------------------------
392 void wxScrollHelper::DeleteEvtHandler()
394 // search for m_handler in the handler list
395 if ( m_win
&& m_handler
)
397 if ( m_win
->RemoveEventHandler(m_handler
) )
401 //else: something is very wrong, so better [maybe] leak memory than
402 // risk a crash because of double deletion
408 void wxScrollHelper::SetWindow(wxWindow
*win
)
410 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
414 // by default, the associated window is also the target window
415 DoSetTargetWindow(win
);
418 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
420 m_targetWindow
= target
;
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 m_targetWindow
->Update() ;
517 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
519 int pos
= event
.GetPosition();
520 int orient
= event
.GetOrientation();
523 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
525 if (orient
== wxHORIZONTAL
)
526 nScrollInc
= - m_xScrollPosition
;
528 nScrollInc
= - m_yScrollPosition
;
530 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
532 if (orient
== wxHORIZONTAL
)
533 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
535 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
537 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
541 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
545 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
547 if (orient
== wxHORIZONTAL
)
548 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
550 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
552 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
554 if (orient
== wxHORIZONTAL
)
555 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
557 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
559 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
560 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
562 if (orient
== wxHORIZONTAL
)
563 nScrollInc
= pos
- m_xScrollPosition
;
565 nScrollInc
= pos
- m_yScrollPosition
;
568 if (orient
== wxHORIZONTAL
)
570 if (m_xScrollPixelsPerLine
> 0)
573 GetTargetSize(&w
, &h
);
575 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
576 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
580 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
581 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
582 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
583 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
586 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
590 if ( m_yScrollPixelsPerLine
> 0 )
592 if ( m_yScrollPosition
+ nScrollInc
< 0 )
594 // As -ve as we can go
595 nScrollInc
= -m_yScrollPosition
;
597 else // check for the other bound
599 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
600 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
602 // As +ve as we can go
603 nScrollInc
= posMax
- m_yScrollPosition
;
609 // VZ: why do we do this? (FIXME)
610 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
617 // Adjust the scrollbars - new version.
618 void wxScrollHelper::AdjustScrollbars()
621 m_targetWindow
->Update();
627 int oldXScroll
= m_xScrollPosition
;
628 int oldYScroll
= m_yScrollPosition
;
630 // VZ: at least under Windows this loop is useless because when scrollbars
631 // [dis]appear we get a WM_SIZE resulting in another call to
632 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
633 // it here for now but it would be better to ensure that all ports
634 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
635 // necessary, and remove it later
638 GetTargetSize(&w
, 0);
640 if (m_xScrollPixelsPerLine
== 0)
643 m_xScrollPosition
= 0;
644 m_win
->SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
648 m_xScrollLines
= m_targetWindow
->GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine
;
650 // Calculate page size i.e. number of scroll units you get on the
651 // current client window
652 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
653 if (noPagePositions
< 1) noPagePositions
= 1;
654 if ( noPagePositions
> m_xScrollLines
)
655 noPagePositions
= m_xScrollLines
;
657 // Correct position if greater than extent of canvas minus
658 // the visible portion of it or if below zero
659 m_xScrollPosition
= wxMin( m_xScrollLines
- noPagePositions
, m_xScrollPosition
);
660 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
662 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
663 // The amount by which we scroll when paging
664 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
667 GetTargetSize(0, &h
);
669 // scroll lines per page: if 0, no scrolling is needed
672 if ( m_yScrollPixelsPerLine
== 0 )
674 // scrolling is disabled
676 m_yScrollPosition
= 0;
679 else // might need scrolling
681 int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
682 m_yScrollLines
= hVirt
/ m_yScrollPixelsPerLine
;
684 // Calculate page size i.e. number of scroll units you get on the
685 // current client window
686 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
687 if ( linesPerPage
>= m_yScrollLines
)
689 // we're big enough to not need scrolling
692 m_yScrollPosition
= 0;
694 else // we do need a scrollbar
696 if ( linesPerPage
< 1 )
699 // Correct position if greater than extent of canvas minus
700 // the visible portion of it or if below zero
701 const int posMax
= m_yScrollLines
- linesPerPage
;
702 if ( m_yScrollPosition
> posMax
)
703 m_yScrollPosition
= posMax
;
704 else if ( m_yScrollPosition
< 0 )
705 m_yScrollPosition
= 0;
709 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
710 linesPerPage
, m_yScrollLines
);
712 // The amount by which we scroll when paging
713 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
716 // If a scrollbar (dis)appeared as a result of this, adjust them again.
720 GetTargetSize( &w
, &h
);
721 } while ( w
!= oldw
&& h
!= oldh
);
724 // Sorry, some Motif-specific code to implement a backing pixmap
725 // for the wxRETAINED style. Implementing a backing store can't
726 // be entirely generic because it relies on the wxWindowDC implementation
727 // to duplicate X drawing calls for the backing pixmap.
729 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
731 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
733 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
734 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
735 if (m_targetWindow
->GetBackingPixmap() &&
736 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
737 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
739 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
740 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
743 if (!m_targetWindow
->GetBackingPixmap() &&
744 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
746 int depth
= wxDisplayDepth();
747 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
748 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
749 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
750 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
756 if (oldXScroll
!= m_xScrollPosition
)
758 if (m_xScrollingEnabled
)
759 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
762 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
765 if (oldYScroll
!= m_yScrollPosition
)
767 if (m_yScrollingEnabled
)
768 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
771 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
775 m_targetWindow
->Update();
779 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
781 wxPoint pt
= dc
.GetDeviceOrigin();
782 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
783 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
784 dc
.SetUserScale( m_scaleX
, m_scaleY
);
787 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
789 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
790 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
792 m_xScrollPixelsPerLine
= xstep
;
793 m_yScrollPixelsPerLine
= ystep
;
795 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
796 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
798 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
799 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
800 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
805 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
808 *x_unit
= m_xScrollPixelsPerLine
;
810 *y_unit
= m_yScrollPixelsPerLine
;
813 int wxScrollHelper::GetScrollPageSize(int orient
) const
815 if ( orient
== wxHORIZONTAL
)
816 return m_xScrollLinesPerPage
;
818 return m_yScrollLinesPerPage
;
821 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
823 if ( orient
== wxHORIZONTAL
)
824 m_xScrollLinesPerPage
= pageSize
;
826 m_yScrollLinesPerPage
= pageSize
;
830 * Scroll to given position (scroll position, not pixel position)
832 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
837 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
838 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
841 m_targetWindow
->Update();
845 GetTargetSize(&w
, &h
);
847 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
849 int old_x
= m_xScrollPosition
;
850 m_xScrollPosition
= x_pos
;
852 // Calculate page size i.e. number of scroll units you get on the
853 // current client window
854 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
855 if (noPagePositions
< 1) noPagePositions
= 1;
857 // Correct position if greater than extent of canvas minus
858 // the visible portion of it or if below zero
859 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
860 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
862 if (old_x
!= m_xScrollPosition
) {
863 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
864 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
868 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
870 int old_y
= m_yScrollPosition
;
871 m_yScrollPosition
= y_pos
;
873 // Calculate page size i.e. number of scroll units you get on the
874 // current client window
875 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
876 if (noPagePositions
< 1) noPagePositions
= 1;
878 // Correct position if greater than extent of canvas minus
879 // the visible portion of it or if below zero
880 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
881 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
883 if (old_y
!= m_yScrollPosition
) {
884 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
885 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
891 m_targetWindow
->Update();
896 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
898 m_xScrollingEnabled
= x_scroll
;
899 m_yScrollingEnabled
= y_scroll
;
902 // Where the current view starts from
903 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
906 *x
= m_xScrollPosition
;
908 *y
= m_yScrollPosition
;
911 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
914 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
916 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
919 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
922 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
924 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
927 // ----------------------------------------------------------------------------
929 // ----------------------------------------------------------------------------
931 // Default OnSize resets scrollbars, if any
932 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
934 if( m_win
->GetAutoLayout() || m_targetWindow
->GetAutoLayout() )
936 if ( m_targetWindow
!= m_win
)
937 m_targetWindow
->FitInside();
941 // FIXME: Something is really weird here... This should be
942 // called by FitInside above (and apparently is), yet the
943 // scrollsub sample will get the scrollbar wrong if resized
944 // quickly. This masks the bug, but is surely not the right
952 // This calls OnDraw, having adjusted the origin according to the current
954 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
956 // don't use m_targetWindow here, this is always called for ourselves
963 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
964 // compatibility here - if we used OnKeyDown(), the programs which process
965 // arrows themselves in their OnChar() would never get the message and like
966 // this they always have the priority
967 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
969 int stx
, sty
, // view origin
970 szx
, szy
, // view size (total)
971 clix
, cliy
; // view size (on screen)
973 GetViewStart(&stx
, &sty
);
974 GetTargetSize(&clix
, &cliy
);
975 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
977 if( m_xScrollPixelsPerLine
)
979 clix
/= m_xScrollPixelsPerLine
;
980 szx
/= m_xScrollPixelsPerLine
;
987 if( m_yScrollPixelsPerLine
)
989 cliy
/= m_yScrollPixelsPerLine
;
990 szy
/= m_yScrollPixelsPerLine
;
998 int xScrollOld
= m_xScrollPosition
,
999 yScrollOld
= m_yScrollPosition
;
1002 switch ( event
.GetKeyCode() )
1006 dsty
= sty
- (5 * cliy
/ 6);
1007 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1012 Scroll(-1, sty
+ (5 * cliy
/ 6));
1016 Scroll(0, event
.ControlDown() ? 0 : -1);
1020 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1024 Scroll(-1, sty
- 1);
1028 Scroll(-1, sty
+ 1);
1032 Scroll(stx
- 1, -1);
1036 Scroll(stx
+ 1, -1);
1044 if ( m_xScrollPosition
!= xScrollOld
)
1046 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1048 event
.SetEventObject(m_win
);
1049 m_win
->GetEventHandler()->ProcessEvent(event
);
1052 if ( m_yScrollPosition
!= yScrollOld
)
1054 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1056 event
.SetEventObject(m_win
);
1057 m_win
->GetEventHandler()->ProcessEvent(event
);
1061 // ----------------------------------------------------------------------------
1062 // autoscroll stuff: these functions deal with sending fake scroll events when
1063 // a captured mouse is being held outside the window
1064 // ----------------------------------------------------------------------------
1066 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1068 // only send the event if the window is scrollable in this direction
1069 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1070 return win
->HasScrollbar(event
.GetOrientation());
1073 void wxScrollHelper::StopAutoScrolling()
1075 if ( m_timerAutoScroll
)
1077 delete m_timerAutoScroll
;
1078 m_timerAutoScroll
= (wxTimer
*)NULL
;
1082 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1084 StopAutoScrolling();
1089 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1091 // don't prevent the usual processing of the event from taking place
1094 // when a captured mouse leave a scrolled window we start generate
1095 // scrolling events to allow, for example, extending selection beyond the
1096 // visible area in some controls
1097 if ( wxWindow::GetCapture() == m_targetWindow
)
1099 // where is the mouse leaving?
1101 wxPoint pt
= event
.GetPosition();
1104 orient
= wxHORIZONTAL
;
1107 else if ( pt
.y
< 0 )
1109 orient
= wxVERTICAL
;
1112 else // we're lower or to the right of the window
1114 wxSize size
= m_targetWindow
->GetClientSize();
1115 if ( pt
.x
> size
.x
)
1117 orient
= wxHORIZONTAL
;
1118 pos
= m_xScrollLines
;
1120 else if ( pt
.y
> size
.y
)
1122 orient
= wxVERTICAL
;
1123 pos
= m_yScrollLines
;
1125 else // this should be impossible
1127 // but seems to happen sometimes under wxMSW - maybe it's a bug
1128 // there but for now just ignore it
1130 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1136 // only start the auto scroll timer if the window can be scrolled in
1138 if ( !m_targetWindow
->HasScrollbar(orient
) )
1141 delete m_timerAutoScroll
;
1142 m_timerAutoScroll
= new wxAutoScrollTimer
1144 m_targetWindow
, this,
1145 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1146 : wxEVT_SCROLLWIN_LINEDOWN
,
1150 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1154 #if wxUSE_MOUSEWHEEL
1156 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1158 m_wheelRotation
+= event
.GetWheelRotation();
1159 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1160 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1165 wxScrollWinEvent newEvent
;
1167 newEvent
.SetPosition(0);
1168 newEvent
.SetOrientation(wxVERTICAL
);
1169 newEvent
.m_eventObject
= m_win
;
1171 if (event
.IsPageScroll())
1174 newEvent
.m_eventType
= wxEVT_SCROLLWIN_PAGEUP
;
1176 newEvent
.m_eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
1178 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1182 lines
*= event
.GetLinesPerAction();
1184 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEUP
;
1186 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
1188 int times
= abs(lines
);
1189 for (; times
> 0; times
--)
1190 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1195 #endif // wxUSE_MOUSEWHEEL
1197 // ----------------------------------------------------------------------------
1198 // wxGenericScrolledWindow implementation
1199 // ----------------------------------------------------------------------------
1201 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1203 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1204 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1207 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1212 const wxString
& name
)
1214 m_targetWindow
= this;
1216 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1221 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1225 bool wxGenericScrolledWindow::Layout()
1227 if (GetSizer() && m_targetWindow
== this)
1229 // If we're the scroll target, take into account the
1230 // virtual size and scrolled position of the window.
1233 CalcScrolledPosition(0,0, &x
,&y
);
1234 GetVirtualSize(&w
, &h
);
1235 GetSizer()->SetDimension(x
, y
, w
, h
);
1239 // fall back to default for LayoutConstraints
1240 return wxPanel::Layout();
1243 void wxGenericScrolledWindow::DoSetVirtualSize(int x
, int y
)
1245 wxPanel::DoSetVirtualSize( x
, y
);
1248 #if wxUSE_CONSTRAINTS
1249 if (GetAutoLayout())
1254 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1256 // the user code didn't really draw the window if we got here, so set this
1257 // flag to try to call OnDraw() later
1258 m_handler
->ResetDrawnFlag();
1265 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1269 long rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1272 // we need to process arrows ourselves for scrolling
1273 if ( nMsg
== WM_GETDLGCODE
)
1275 rc
|= DLGC_WANTARROWS
;
1284 #if WXWIN_COMPATIBILITY
1286 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
1288 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
1289 *y_page
= GetScrollPageSize(wxVERTICAL
);
1292 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
1295 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
1297 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
1300 #endif // WXWIN_COMPATIBILITY