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 license
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
)
64 // ----------------------------------------------------------------------------
65 // wxScrollHelperEvtHandler: intercept the events from the window and forward
66 // them to wxScrollHelper
67 // ----------------------------------------------------------------------------
69 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
72 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
74 m_scrollHelper
= scrollHelper
;
77 virtual bool ProcessEvent(wxEvent
& event
);
79 void ResetDrawnFlag() { m_hasDrawnWindow
= FALSE
; }
82 wxScrollHelper
*m_scrollHelper
;
84 bool m_hasDrawnWindow
;
87 // ----------------------------------------------------------------------------
88 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
89 // a captured mouse is held outside the window
90 // ----------------------------------------------------------------------------
92 class wxAutoScrollTimer
: public wxTimer
95 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
96 wxEventType eventTypeToSend
,
99 virtual void Notify();
103 wxScrollHelper
*m_scrollHelper
;
104 wxEventType m_eventType
;
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
118 wxScrollHelper
*scroll
,
119 wxEventType eventTypeToSend
,
123 m_scrollHelper
= scroll
;
124 m_eventType
= eventTypeToSend
;
129 void wxAutoScrollTimer::Notify()
131 // only do all this as long as the window is capturing the mouse
132 if ( wxWindow::GetCapture() != m_win
)
136 else // we still capture the mouse, continue generating events
138 // first scroll the window if we are allowed to do it
139 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
140 event1
.SetEventObject(m_win
);
141 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
142 m_win
->GetEventHandler()->ProcessEvent(event1
) )
144 // and then send a pseudo mouse-move event to refresh the selection
145 wxMouseEvent
event2(wxEVT_MOTION
);
146 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
148 // the mouse event coordinates should be client, not screen as
149 // returned by wxGetMousePosition
150 wxWindow
*parentTop
= m_win
;
151 while ( parentTop
->GetParent() )
152 parentTop
= parentTop
->GetParent();
153 wxPoint ptOrig
= parentTop
->GetPosition();
154 event2
.m_x
-= ptOrig
.x
;
155 event2
.m_y
-= ptOrig
.y
;
157 event2
.SetEventObject(m_win
);
159 // FIXME: we don't fill in the other members - ok?
161 m_win
->GetEventHandler()->ProcessEvent(event2
);
163 else // can't scroll further, stop
170 // ----------------------------------------------------------------------------
171 // wxScrollHelperEvtHandler
172 // ----------------------------------------------------------------------------
174 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
176 wxEventType evType
= event
.GetEventType();
178 // the explanation of wxEVT_PAINT processing hack: for historic reasons
179 // there are 2 ways to process this event in classes deriving from
180 // wxScrolledWindow. The user code may
182 // 1. override wxScrolledWindow::OnDraw(dc)
183 // 2. define its own OnPaint() handler
185 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
186 // always processes the draw event, so we can't just try the window
187 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
188 // (the latter would never be called in wxUniversal).
190 // So the solution is to have a flag telling us whether the user code drew
191 // anything in the window. We set it to true here but reset it to false in
192 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
193 // user code defined OnPaint() in the derived class)
194 m_hasDrawnWindow
= TRUE
;
196 // pass it on to the real handler
197 bool processed
= wxEvtHandler::ProcessEvent(event
);
199 // always process the size events ourselves, even if the user code handles
200 // them as well, as we need to AdjustScrollbars()
202 // NB: it is important to do it after processing the event in the normal
203 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
204 // scrollbar[s] (dis)appear and it should be seen by the user code
206 if ( evType
== wxEVT_SIZE
)
208 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
215 // normally, nothing more to do here - except if it was a paint event
216 // which wasn't really processed, then we'll try to call our
217 // OnDraw() below (from HandleOnPaint)
218 if ( m_hasDrawnWindow
)
224 // reset the skipped flag to FALSE as it might have been set to TRUE in
225 // ProcessEvent() above
228 if ( evType
== wxEVT_PAINT
)
230 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
234 if ( evType
== wxEVT_SCROLLWIN_TOP
||
235 evType
== wxEVT_SCROLLWIN_BOTTOM
||
236 evType
== wxEVT_SCROLLWIN_LINEUP
||
237 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
238 evType
== wxEVT_SCROLLWIN_PAGEUP
||
239 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
240 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
241 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
243 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
244 return !event
.GetSkipped();
247 if ( evType
== wxEVT_ENTER_WINDOW
)
249 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
251 else if ( evType
== wxEVT_LEAVE_WINDOW
)
253 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
256 else if ( evType
== wxEVT_MOUSEWHEEL
)
258 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
260 #endif // wxUSE_MOUSEWHEEL
261 else if ( evType
== wxEVT_CHAR
)
263 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
264 return !event
.GetSkipped();
270 // ----------------------------------------------------------------------------
271 // wxScrollHelper construction
272 // ----------------------------------------------------------------------------
274 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
276 m_xScrollPixelsPerLine
=
277 m_yScrollPixelsPerLine
=
282 m_xScrollLinesPerPage
=
283 m_yScrollLinesPerPage
= 0;
285 m_xScrollingEnabled
=
286 m_yScrollingEnabled
= TRUE
;
295 m_targetWindow
= (wxWindow
*)NULL
;
297 m_timerAutoScroll
= (wxTimer
*)NULL
;
305 wxScrollHelper::~wxScrollHelper()
312 // ----------------------------------------------------------------------------
313 // setting scrolling parameters
314 // ----------------------------------------------------------------------------
316 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
326 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
329 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
330 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
332 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
333 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
334 (xPos
!= m_xScrollPosition
) ||
335 (yPos
!= m_yScrollPosition
)
338 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
339 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
340 m_xScrollPosition
= xPos
;
341 m_yScrollPosition
= yPos
;
343 // For better backward compatibility we set persisting limits
344 // here not just the size. It makes SetScrollbars 'sticky'
345 // emulating the old non-autoscroll behaviour.
347 m_targetWindow
->SetVirtualSizeHints( noUnitsX
* pixelsPerUnitX
, noUnitsY
* pixelsPerUnitY
);
349 // The above should arguably be deprecated, this however we still need.
351 m_targetWindow
->SetVirtualSize( noUnitsX
* pixelsPerUnitX
, noUnitsY
* pixelsPerUnitY
);
353 if (do_refresh
&& !noRefresh
)
354 m_targetWindow
->Refresh(TRUE
, GetRect());
357 m_targetWindow
->MacUpdateImmediately() ;
361 // ----------------------------------------------------------------------------
362 // [target] window handling
363 // ----------------------------------------------------------------------------
365 void wxScrollHelper::DeleteEvtHandler()
367 // search for m_handler in the handler list
368 if ( m_win
&& m_handler
)
370 if ( m_win
->RemoveEventHandler(m_handler
) )
374 //else: something is very wrong, so better [maybe] leak memory than
375 // risk a crash because of double deletion
381 void wxScrollHelper::SetWindow(wxWindow
*win
)
383 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
387 // by default, the associated window is also the target window
388 DoSetTargetWindow(win
);
391 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
393 m_targetWindow
= target
;
395 // install the event handler which will intercept the events we're
396 // interested in (but only do it for our real window, not the target window
397 // which we scroll - we don't need to hijack its events)
398 if ( m_targetWindow
== m_win
)
400 // if we already have a handler, delete it first
403 m_handler
= new wxScrollHelperEvtHandler(this);
404 m_targetWindow
->PushEventHandler(m_handler
);
408 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
410 wxCHECK_RET( target
, wxT("target window must not be NULL") );
412 if ( target
== m_targetWindow
)
415 DoSetTargetWindow(target
);
418 wxWindow
*wxScrollHelper::GetTargetWindow() const
420 return m_targetWindow
;
423 // ----------------------------------------------------------------------------
424 // scrolling implementation itself
425 // ----------------------------------------------------------------------------
427 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
429 int nScrollInc
= CalcScrollInc(event
);
430 if ( nScrollInc
== 0 )
432 // can't scroll further
438 int orient
= event
.GetOrientation();
439 if (orient
== wxHORIZONTAL
)
441 m_xScrollPosition
+= nScrollInc
;
442 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
446 m_yScrollPosition
+= nScrollInc
;
447 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
450 bool needsRefresh
= FALSE
;
453 if (orient
== wxHORIZONTAL
)
455 if ( m_xScrollingEnabled
)
457 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
466 if ( m_yScrollingEnabled
)
468 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
478 m_targetWindow
->Refresh(TRUE
, GetRect());
482 m_targetWindow
->ScrollWindow(dx
, dy
, GetRect());
486 m_targetWindow
->MacUpdateImmediately() ;
490 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
492 int pos
= event
.GetPosition();
493 int orient
= event
.GetOrientation();
496 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
498 if (orient
== wxHORIZONTAL
)
499 nScrollInc
= - m_xScrollPosition
;
501 nScrollInc
= - m_yScrollPosition
;
503 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
505 if (orient
== wxHORIZONTAL
)
506 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
508 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
510 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
514 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
518 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
520 if (orient
== wxHORIZONTAL
)
521 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
523 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
525 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
527 if (orient
== wxHORIZONTAL
)
528 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
530 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
532 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
533 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
535 if (orient
== wxHORIZONTAL
)
536 nScrollInc
= pos
- m_xScrollPosition
;
538 nScrollInc
= pos
- m_yScrollPosition
;
541 if (orient
== wxHORIZONTAL
)
543 if (m_xScrollPixelsPerLine
> 0)
546 GetTargetSize(&w
, &h
);
548 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
549 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
553 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
554 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
555 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
556 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
559 m_targetWindow
->Refresh(TRUE
, GetRect());
563 if (m_yScrollPixelsPerLine
> 0)
566 GetTargetSize(&w
, &h
);
568 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
569 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
573 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
574 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
575 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
576 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
579 m_targetWindow
->Refresh(TRUE
, GetRect());
585 // Adjust the scrollbars - new version.
586 void wxScrollHelper::AdjustScrollbars()
589 m_targetWindow
->MacUpdateImmediately();
595 int oldXScroll
= m_xScrollPosition
;
596 int oldYScroll
= m_yScrollPosition
;
599 GetTargetSize(&w
, 0);
601 if (m_xScrollPixelsPerLine
== 0)
604 m_xScrollPosition
= 0;
605 m_win
->SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
609 m_xScrollLines
= m_targetWindow
->GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine
;
611 // Calculate page size i.e. number of scroll units you get on the
612 // current client window
613 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
614 if (noPagePositions
< 1) noPagePositions
= 1;
615 if ( noPagePositions
> m_xScrollLines
)
616 noPagePositions
= m_xScrollLines
;
618 // Correct position if greater than extent of canvas minus
619 // the visible portion of it or if below zero
620 m_xScrollPosition
= wxMin( m_xScrollLines
- noPagePositions
, m_xScrollPosition
);
621 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
623 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
624 // The amount by which we scroll when paging
625 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
628 GetTargetSize(0, &h
);
630 if (m_yScrollPixelsPerLine
== 0)
633 m_yScrollPosition
= 0;
634 m_win
->SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
638 m_yScrollLines
= m_targetWindow
->GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine
;
640 // Calculate page size i.e. number of scroll units you get on the
641 // current client window
642 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
643 if (noPagePositions
< 1) noPagePositions
= 1;
644 if ( noPagePositions
> m_yScrollLines
)
645 noPagePositions
= m_yScrollLines
;
647 // Correct position if greater than extent of canvas minus
648 // the visible portion of it or if below zero
649 m_yScrollPosition
= wxMin( m_yScrollLines
- noPagePositions
, m_yScrollPosition
);
650 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
652 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
653 // The amount by which we scroll when paging
654 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
657 // If a scrollbar (dis)appeared as a result of this, adjust them again.
662 GetTargetSize( &w
, &h
);
663 } while ( w
!= oldw
&& h
!= oldh
);
666 // Sorry, some Motif-specific code to implement a backing pixmap
667 // for the wxRETAINED style. Implementing a backing store can't
668 // be entirely generic because it relies on the wxWindowDC implementation
669 // to duplicate X drawing calls for the backing pixmap.
671 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
673 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
675 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
676 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
677 if (m_targetWindow
->GetBackingPixmap() &&
678 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
679 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
681 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
682 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
685 if (!m_targetWindow
->GetBackingPixmap() &&
686 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
688 int depth
= wxDisplayDepth();
689 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
690 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
691 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
692 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
698 if (oldXScroll
!= m_xScrollPosition
)
700 if (m_xScrollingEnabled
)
701 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
704 m_targetWindow
->Refresh(TRUE
, GetRect());
707 if (oldYScroll
!= m_yScrollPosition
)
709 if (m_yScrollingEnabled
)
710 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
713 m_targetWindow
->Refresh(TRUE
, GetRect());
717 m_targetWindow
->MacUpdateImmediately();
721 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
723 wxPoint pt
= dc
.GetDeviceOrigin();
724 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
725 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
726 dc
.SetUserScale( m_scaleX
, m_scaleY
);
729 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
731 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
732 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
734 m_xScrollPixelsPerLine
= xstep
;
735 m_yScrollPixelsPerLine
= ystep
;
737 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
738 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
740 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
741 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
742 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
747 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
750 *x_unit
= m_xScrollPixelsPerLine
;
752 *y_unit
= m_yScrollPixelsPerLine
;
755 int wxScrollHelper::GetScrollPageSize(int orient
) const
757 if ( orient
== wxHORIZONTAL
)
758 return m_xScrollLinesPerPage
;
760 return m_yScrollLinesPerPage
;
763 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
765 if ( orient
== wxHORIZONTAL
)
766 m_xScrollLinesPerPage
= pageSize
;
768 m_yScrollLinesPerPage
= pageSize
;
772 * Scroll to given position (scroll position, not pixel position)
774 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
779 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
780 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
783 m_targetWindow
->MacUpdateImmediately();
787 GetTargetSize(&w
, &h
);
789 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
791 int old_x
= m_xScrollPosition
;
792 m_xScrollPosition
= x_pos
;
794 // Calculate page size i.e. number of scroll units you get on the
795 // current client window
796 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
797 if (noPagePositions
< 1) noPagePositions
= 1;
799 // Correct position if greater than extent of canvas minus
800 // the visible portion of it or if below zero
801 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
802 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
804 if (old_x
!= m_xScrollPosition
) {
805 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
806 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
810 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
812 int old_y
= m_yScrollPosition
;
813 m_yScrollPosition
= y_pos
;
815 // Calculate page size i.e. number of scroll units you get on the
816 // current client window
817 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
818 if (noPagePositions
< 1) noPagePositions
= 1;
820 // Correct position if greater than extent of canvas minus
821 // the visible portion of it or if below zero
822 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
823 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
825 if (old_y
!= m_yScrollPosition
) {
826 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
827 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
833 m_targetWindow
->MacUpdateImmediately();
838 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
840 m_xScrollingEnabled
= x_scroll
;
841 m_yScrollingEnabled
= y_scroll
;
844 // Where the current view starts from
845 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
848 *x
= m_xScrollPosition
;
850 *y
= m_yScrollPosition
;
853 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
856 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
858 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
861 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
864 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
866 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
869 // ----------------------------------------------------------------------------
871 // ----------------------------------------------------------------------------
873 // Default OnSize resets scrollbars, if any
874 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
876 if( m_win
->GetAutoLayout() )
878 if ( m_targetWindow
!= m_win
)
879 m_targetWindow
->FitInside();
883 #if wxUSE_CONSTRAINTS
891 // This calls OnDraw, having adjusted the origin according to the current
893 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
895 // don't use m_targetWindow here, this is always called for ourselves
902 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
903 // compatibility here - if we used OnKeyDown(), the programs which process
904 // arrows themselves in their OnChar() would never get the message and like
905 // this they always have the priority
906 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
908 int stx
, sty
, // view origin
909 szx
, szy
, // view size (total)
910 clix
, cliy
; // view size (on screen)
912 GetViewStart(&stx
, &sty
);
913 GetTargetSize(&clix
, &cliy
);
914 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
916 if( m_xScrollPixelsPerLine
)
918 clix
/= m_xScrollPixelsPerLine
;
919 szx
/= m_xScrollPixelsPerLine
;
926 if( m_yScrollPixelsPerLine
)
928 cliy
/= m_yScrollPixelsPerLine
;
929 szy
/= m_yScrollPixelsPerLine
;
937 int xScrollOld
= m_xScrollPosition
,
938 yScrollOld
= m_yScrollPosition
;
941 switch ( event
.KeyCode() )
945 dsty
= sty
- (5 * cliy
/ 6);
946 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
951 Scroll(-1, sty
+ (5 * cliy
/ 6));
955 Scroll(0, event
.ControlDown() ? 0 : -1);
959 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
983 if ( m_xScrollPosition
!= xScrollOld
)
985 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
987 event
.SetEventObject(m_win
);
988 m_win
->GetEventHandler()->ProcessEvent(event
);
991 if ( m_yScrollPosition
!= yScrollOld
)
993 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
995 event
.SetEventObject(m_win
);
996 m_win
->GetEventHandler()->ProcessEvent(event
);
1000 // ----------------------------------------------------------------------------
1001 // autoscroll stuff: these functions deal with sending fake scroll events when
1002 // a captured mouse is being held outside the window
1003 // ----------------------------------------------------------------------------
1005 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1007 // only send the event if the window is scrollable in this direction
1008 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1009 return win
->HasScrollbar(event
.GetOrientation());
1012 void wxScrollHelper::StopAutoScrolling()
1014 if ( m_timerAutoScroll
)
1016 delete m_timerAutoScroll
;
1017 m_timerAutoScroll
= (wxTimer
*)NULL
;
1021 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1023 StopAutoScrolling();
1028 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1030 // don't prevent the usual processing of the event from taking place
1033 // when a captured mouse leave a scrolled window we start generate
1034 // scrolling events to allow, for example, extending selection beyond the
1035 // visible area in some controls
1036 if ( wxWindow::GetCapture() == m_targetWindow
)
1038 // where is the mouse leaving?
1040 wxPoint pt
= event
.GetPosition();
1043 orient
= wxHORIZONTAL
;
1046 else if ( pt
.y
< 0 )
1048 orient
= wxVERTICAL
;
1051 else // we're lower or to the right of the window
1053 wxSize size
= m_targetWindow
->GetClientSize();
1054 if ( pt
.x
> size
.x
)
1056 orient
= wxHORIZONTAL
;
1057 pos
= m_xScrollLines
;
1059 else if ( pt
.y
> size
.y
)
1061 orient
= wxVERTICAL
;
1062 pos
= m_yScrollLines
;
1064 else // this should be impossible
1066 // but seems to happen sometimes under wxMSW - maybe it's a bug
1067 // there but for now just ignore it
1069 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1075 // only start the auto scroll timer if the window can be scrolled in
1077 if ( !m_targetWindow
->HasScrollbar(orient
) )
1080 delete m_timerAutoScroll
;
1081 m_timerAutoScroll
= new wxAutoScrollTimer
1083 m_targetWindow
, this,
1084 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1085 : wxEVT_SCROLLWIN_LINEDOWN
,
1089 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1093 #if wxUSE_MOUSEWHEEL
1095 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1097 m_wheelRotation
+= event
.GetWheelRotation();
1098 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1099 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1104 wxScrollWinEvent newEvent
;
1106 newEvent
.SetPosition(0);
1107 newEvent
.SetOrientation(wxVERTICAL
);
1108 newEvent
.m_eventObject
= m_win
;
1110 if (event
.IsPageScroll())
1113 newEvent
.m_eventType
= wxEVT_SCROLLWIN_PAGEUP
;
1115 newEvent
.m_eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
1117 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1121 lines
*= event
.GetLinesPerAction();
1123 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEUP
;
1125 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
1127 int times
= abs(lines
);
1128 for (; times
> 0; times
--)
1129 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1134 #endif // wxUSE_MOUSEWHEEL
1136 // ----------------------------------------------------------------------------
1137 // wxGenericScrolledWindow implementation
1138 // ----------------------------------------------------------------------------
1140 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1142 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1143 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1146 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1151 const wxString
& name
)
1153 m_targetWindow
= this;
1155 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1160 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1164 bool wxGenericScrolledWindow::Layout()
1166 if (GetSizer() && m_targetWindow
== this)
1168 // If we're the scroll target, take into account the
1169 // virtual size and scrolled position of the window.
1172 CalcScrolledPosition(0,0, &x
,&y
);
1173 GetVirtualSize(&w
, &h
);
1174 GetSizer()->SetDimension(x
, y
, w
, h
);
1178 // fall back to default for LayoutConstraints
1179 return wxPanel::Layout();
1182 void wxGenericScrolledWindow::DoSetVirtualSize(int x
, int y
)
1184 wxPanel::DoSetVirtualSize( x
, y
);
1187 #if wxUSE_CONSTRAINTS
1188 if (GetAutoLayout())
1193 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1195 // the user code didn't really draw the window if we got here, so set this
1196 // flag to try to call OnDraw() later
1197 m_handler
->ResetDrawnFlag();
1204 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1208 long rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1210 // we need to process arrows ourselves for scrolling
1211 if ( nMsg
== WM_GETDLGCODE
)
1213 rc
|= DLGC_WANTARROWS
;
1221 #if WXWIN_COMPATIBILITY
1223 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
1225 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
1226 *y_page
= GetScrollPageSize(wxVERTICAL
);
1229 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
1232 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
1234 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
1237 #endif // WXWIN_COMPATIBILITY