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 m_targetWindow
->SetVirtualSizeHints( noUnitsX
* pixelsPerUnitX
, noUnitsY
* pixelsPerUnitY
);
345 if (do_refresh
&& !noRefresh
)
346 m_targetWindow
->Refresh(TRUE
, GetRect());
349 m_targetWindow
->MacUpdateImmediately() ;
353 // ----------------------------------------------------------------------------
354 // [target] window handling
355 // ----------------------------------------------------------------------------
357 void wxScrollHelper::DeleteEvtHandler()
359 // search for m_handler in the handler list
360 if ( m_win
&& m_handler
)
362 if ( m_win
->RemoveEventHandler(m_handler
) )
366 //else: something is very wrong, so better [maybe] leak memory than
367 // risk a crash because of double deletion
373 void wxScrollHelper::SetWindow(wxWindow
*win
)
375 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
379 // by default, the associated window is also the target window
380 DoSetTargetWindow(win
);
383 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
385 m_targetWindow
= target
;
387 // install the event handler which will intercept the events we're
388 // interested in (but only do it for our real window, not the target window
389 // which we scroll - we don't need to hijack its events)
390 if ( m_targetWindow
== m_win
)
392 // if we already have a handler, delete it first
395 m_handler
= new wxScrollHelperEvtHandler(this);
396 m_targetWindow
->PushEventHandler(m_handler
);
400 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
402 wxCHECK_RET( target
, wxT("target window must not be NULL") );
404 if ( target
== m_targetWindow
)
407 DoSetTargetWindow(target
);
410 wxWindow
*wxScrollHelper::GetTargetWindow() const
412 return m_targetWindow
;
415 // ----------------------------------------------------------------------------
416 // scrolling implementation itself
417 // ----------------------------------------------------------------------------
419 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
421 int nScrollInc
= CalcScrollInc(event
);
422 if ( nScrollInc
== 0 )
424 // can't scroll further
430 int orient
= event
.GetOrientation();
431 if (orient
== wxHORIZONTAL
)
433 m_xScrollPosition
+= nScrollInc
;
434 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
438 m_yScrollPosition
+= nScrollInc
;
439 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
442 bool needsRefresh
= FALSE
;
445 if (orient
== wxHORIZONTAL
)
447 if ( m_xScrollingEnabled
)
449 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
458 if ( m_yScrollingEnabled
)
460 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
470 m_targetWindow
->Refresh(TRUE
, GetRect());
474 m_targetWindow
->ScrollWindow(dx
, dy
, GetRect());
478 m_targetWindow
->MacUpdateImmediately() ;
482 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
484 int pos
= event
.GetPosition();
485 int orient
= event
.GetOrientation();
488 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
490 if (orient
== wxHORIZONTAL
)
491 nScrollInc
= - m_xScrollPosition
;
493 nScrollInc
= - m_yScrollPosition
;
495 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
497 if (orient
== wxHORIZONTAL
)
498 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
500 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
502 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
506 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
510 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
512 if (orient
== wxHORIZONTAL
)
513 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
515 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
517 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
519 if (orient
== wxHORIZONTAL
)
520 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
522 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
524 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
525 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
527 if (orient
== wxHORIZONTAL
)
528 nScrollInc
= pos
- m_xScrollPosition
;
530 nScrollInc
= pos
- m_yScrollPosition
;
533 if (orient
== wxHORIZONTAL
)
535 if (m_xScrollPixelsPerLine
> 0)
538 GetTargetSize(&w
, &h
);
540 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
541 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
545 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
546 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
547 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
548 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
551 m_targetWindow
->Refresh(TRUE
, GetRect());
555 if (m_yScrollPixelsPerLine
> 0)
558 GetTargetSize(&w
, &h
);
560 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
561 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
565 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
566 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
567 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
568 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
571 m_targetWindow
->Refresh(TRUE
, GetRect());
577 // Adjust the scrollbars - new version.
578 void wxScrollHelper::AdjustScrollbars()
581 m_targetWindow
->MacUpdateImmediately();
587 int oldXScroll
= m_xScrollPosition
;
588 int oldYScroll
= m_yScrollPosition
;
591 GetTargetSize(&w
, 0);
593 if (m_xScrollPixelsPerLine
== 0)
596 m_xScrollPosition
= 0;
597 m_win
->SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
601 m_xScrollLines
= m_targetWindow
->GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine
;
603 // Calculate page size i.e. number of scroll units you get on the
604 // current client window
605 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
606 if (noPagePositions
< 1) noPagePositions
= 1;
607 if ( noPagePositions
> m_xScrollLines
)
608 noPagePositions
= m_xScrollLines
;
610 // Correct position if greater than extent of canvas minus
611 // the visible portion of it or if below zero
612 m_xScrollPosition
= wxMin( m_xScrollLines
- noPagePositions
, m_xScrollPosition
);
613 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
615 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
616 // The amount by which we scroll when paging
617 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
620 GetTargetSize(0, &h
);
622 if (m_yScrollPixelsPerLine
== 0)
625 m_yScrollPosition
= 0;
626 m_win
->SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
630 m_yScrollLines
= m_targetWindow
->GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine
;
632 // Calculate page size i.e. number of scroll units you get on the
633 // current client window
634 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
635 if (noPagePositions
< 1) noPagePositions
= 1;
636 if ( noPagePositions
> m_yScrollLines
)
637 noPagePositions
= m_yScrollLines
;
639 // Correct position if greater than extent of canvas minus
640 // the visible portion of it or if below zero
641 m_yScrollPosition
= wxMin( m_yScrollLines
- noPagePositions
, m_yScrollPosition
);
642 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
644 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
645 // The amount by which we scroll when paging
646 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
649 // If a scrollbar (dis)appeared as a result of this, adjust them again.
654 GetTargetSize( &w
, &h
);
655 } while ( w
!= oldw
&& h
!= oldh
);
658 // Sorry, some Motif-specific code to implement a backing pixmap
659 // for the wxRETAINED style. Implementing a backing store can't
660 // be entirely generic because it relies on the wxWindowDC implementation
661 // to duplicate X drawing calls for the backing pixmap.
663 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
665 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
667 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
668 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
669 if (m_targetWindow
->GetBackingPixmap() &&
670 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
671 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
673 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
674 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
677 if (!m_targetWindow
->GetBackingPixmap() &&
678 (noUnitsX
!= 0) && (noUnitsY
!= 0))
680 int depth
= wxDisplayDepth();
681 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
682 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
683 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
684 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
690 if (oldXScroll
!= m_xScrollPosition
)
692 if (m_xScrollingEnabled
)
693 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
696 m_targetWindow
->Refresh(TRUE
, GetRect());
699 if (oldYScroll
!= m_yScrollPosition
)
701 if (m_yScrollingEnabled
)
702 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
705 m_targetWindow
->Refresh(TRUE
, GetRect());
709 m_targetWindow
->MacUpdateImmediately();
713 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
715 wxPoint pt
= dc
.GetDeviceOrigin();
716 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
717 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
718 dc
.SetUserScale( m_scaleX
, m_scaleY
);
721 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
723 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
724 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
726 m_xScrollPixelsPerLine
= xstep
;
727 m_yScrollPixelsPerLine
= ystep
;
729 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
730 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
732 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
733 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
734 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
739 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
742 *x_unit
= m_xScrollPixelsPerLine
;
744 *y_unit
= m_yScrollPixelsPerLine
;
747 int wxScrollHelper::GetScrollPageSize(int orient
) const
749 if ( orient
== wxHORIZONTAL
)
750 return m_xScrollLinesPerPage
;
752 return m_yScrollLinesPerPage
;
755 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
757 if ( orient
== wxHORIZONTAL
)
758 m_xScrollLinesPerPage
= pageSize
;
760 m_yScrollLinesPerPage
= pageSize
;
764 * Scroll to given position (scroll position, not pixel position)
766 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
771 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
772 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
775 m_targetWindow
->MacUpdateImmediately();
779 GetTargetSize(&w
, &h
);
781 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
783 int old_x
= m_xScrollPosition
;
784 m_xScrollPosition
= x_pos
;
786 // Calculate page size i.e. number of scroll units you get on the
787 // current client window
788 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
789 if (noPagePositions
< 1) noPagePositions
= 1;
791 // Correct position if greater than extent of canvas minus
792 // the visible portion of it or if below zero
793 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
794 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
796 if (old_x
!= m_xScrollPosition
) {
797 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
798 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
802 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
804 int old_y
= m_yScrollPosition
;
805 m_yScrollPosition
= y_pos
;
807 // Calculate page size i.e. number of scroll units you get on the
808 // current client window
809 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
810 if (noPagePositions
< 1) noPagePositions
= 1;
812 // Correct position if greater than extent of canvas minus
813 // the visible portion of it or if below zero
814 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
815 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
817 if (old_y
!= m_yScrollPosition
) {
818 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
819 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
825 m_targetWindow
->MacUpdateImmediately();
830 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
832 m_xScrollingEnabled
= x_scroll
;
833 m_yScrollingEnabled
= y_scroll
;
836 // Where the current view starts from
837 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
840 *x
= m_xScrollPosition
;
842 *y
= m_yScrollPosition
;
845 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
848 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
850 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
853 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
856 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
858 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
861 // ----------------------------------------------------------------------------
863 // ----------------------------------------------------------------------------
865 // Default OnSize resets scrollbars, if any
866 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
868 if( m_targetWindow
!= m_win
)
869 m_targetWindow
->SetVirtualSize( m_targetWindow
->GetClientSize() );
871 m_win
->SetVirtualSize( m_win
->GetClientSize() );
873 #if wxUSE_CONSTRAINTS
874 if (m_win
->GetAutoLayout())
879 // This calls OnDraw, having adjusted the origin according to the current
881 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
883 // don't use m_targetWindow here, this is always called for ourselves
890 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
891 // compatibility here - if we used OnKeyDown(), the programs which process
892 // arrows themselves in their OnChar() would never get the message and like
893 // this they always have the priority
894 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
896 int stx
, sty
, // view origin
897 szx
, szy
, // view size (total)
898 clix
, cliy
; // view size (on screen)
900 GetViewStart(&stx
, &sty
);
901 GetTargetSize(&clix
, &cliy
);
902 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
904 if( m_xScrollPixelsPerLine
)
906 clix
/= m_xScrollPixelsPerLine
;
907 szx
/= m_xScrollPixelsPerLine
;
914 if( m_yScrollPixelsPerLine
)
916 cliy
/= m_yScrollPixelsPerLine
;
917 szy
/= m_yScrollPixelsPerLine
;
925 int xScrollOld
= m_xScrollPosition
,
926 yScrollOld
= m_yScrollPosition
;
929 switch ( event
.KeyCode() )
933 dsty
= sty
- (5 * cliy
/ 6);
934 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
939 Scroll(-1, sty
+ (5 * cliy
/ 6));
943 Scroll(0, event
.ControlDown() ? 0 : -1);
947 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
971 if ( m_xScrollPosition
!= xScrollOld
)
973 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
975 event
.SetEventObject(m_win
);
976 m_win
->GetEventHandler()->ProcessEvent(event
);
979 if ( m_yScrollPosition
!= yScrollOld
)
981 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
983 event
.SetEventObject(m_win
);
984 m_win
->GetEventHandler()->ProcessEvent(event
);
988 // ----------------------------------------------------------------------------
989 // autoscroll stuff: these functions deal with sending fake scroll events when
990 // a captured mouse is being held outside the window
991 // ----------------------------------------------------------------------------
993 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
995 // only send the event if the window is scrollable in this direction
996 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
997 return win
->HasScrollbar(event
.GetOrientation());
1000 void wxScrollHelper::StopAutoScrolling()
1002 if ( m_timerAutoScroll
)
1004 delete m_timerAutoScroll
;
1005 m_timerAutoScroll
= (wxTimer
*)NULL
;
1009 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1011 StopAutoScrolling();
1016 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1018 // don't prevent the usual processing of the event from taking place
1021 // when a captured mouse leave a scrolled window we start generate
1022 // scrolling events to allow, for example, extending selection beyond the
1023 // visible area in some controls
1024 if ( wxWindow::GetCapture() == m_targetWindow
)
1026 // where is the mouse leaving?
1028 wxPoint pt
= event
.GetPosition();
1031 orient
= wxHORIZONTAL
;
1034 else if ( pt
.y
< 0 )
1036 orient
= wxVERTICAL
;
1039 else // we're lower or to the right of the window
1041 wxSize size
= m_targetWindow
->GetClientSize();
1042 if ( pt
.x
> size
.x
)
1044 orient
= wxHORIZONTAL
;
1045 pos
= m_xScrollLines
;
1047 else if ( pt
.y
> size
.y
)
1049 orient
= wxVERTICAL
;
1050 pos
= m_yScrollLines
;
1052 else // this should be impossible
1054 // but seems to happen sometimes under wxMSW - maybe it's a bug
1055 // there but for now just ignore it
1057 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1063 // only start the auto scroll timer if the window can be scrolled in
1065 if ( !m_targetWindow
->HasScrollbar(orient
) )
1068 delete m_timerAutoScroll
;
1069 m_timerAutoScroll
= new wxAutoScrollTimer
1071 m_targetWindow
, this,
1072 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1073 : wxEVT_SCROLLWIN_LINEDOWN
,
1077 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1081 #if wxUSE_MOUSEWHEEL
1083 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1085 m_wheelRotation
+= event
.GetWheelRotation();
1086 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1087 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1091 lines
*= event
.GetLinesPerAction();
1093 wxScrollWinEvent newEvent
;
1095 newEvent
.SetPosition(0);
1096 newEvent
.SetOrientation(wxVERTICAL
);
1097 newEvent
.m_eventObject
= m_win
;
1099 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEUP
;
1101 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
1103 int times
= abs(lines
);
1104 for (; times
> 0; times
--)
1105 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1109 // GetViewStart(&vsx, &vsy);
1110 // Scroll(-1, vsy - lines);
1114 #endif // wxUSE_MOUSEWHEEL
1116 // ----------------------------------------------------------------------------
1117 // wxGenericScrolledWindow implementation
1118 // ----------------------------------------------------------------------------
1120 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1122 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1123 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1126 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1131 const wxString
& name
)
1133 m_targetWindow
= this;
1135 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1140 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1144 bool wxGenericScrolledWindow::Layout()
1146 if (GetSizer() && m_targetWindow
== this)
1148 // If we're the scroll target, take into account the
1149 // virtual size and scrolled position of the window.
1152 CalcScrolledPosition(0,0, &x
,&y
);
1153 GetVirtualSize(&w
, &h
);
1154 GetSizer()->SetDimension(x
, y
, w
, h
);
1158 // fall back to default for LayoutConstraints
1159 return wxPanel::Layout();
1162 void wxGenericScrolledWindow::DoSetVirtualSize( int x
, int y
)
1164 wxPanel::DoSetVirtualSize( x
, y
);
1168 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1170 // the user code didn't really draw the window if we got here, so set this
1171 // flag to try to call OnDraw() later
1172 m_handler
->ResetDrawnFlag();
1179 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1183 long rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1185 // we need to process arrows ourselves for scrolling
1186 if ( nMsg
== WM_GETDLGCODE
)
1188 rc
|= DLGC_WANTARROWS
;
1196 #if WXWIN_COMPATIBILITY
1198 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
1200 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
1201 *y_page
= GetScrollPageSize(wxVERTICAL
);
1204 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
1207 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
1209 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
1212 #endif // WXWIN_COMPATIBILITY