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
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "genscrolwin.h"
25 #define XtDisplay XTDISPLAY
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
35 #if !defined(__WXGTK__) || defined(__WXUNIVERSAL__)
38 #include "wx/dcclient.h"
40 #include "wx/scrolwin.h"
45 #include <windows.h> // for DLGC_WANTARROWS
49 // For wxRETAINED implementation
50 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
51 //This code switches off the compiler warnings
52 # pragma message disable nosimpint
56 # pragma message enable nosimpint
60 IMPLEMENT_CLASS(wxScrolledWindow
, wxGenericScrolledWindow
)
62 // ----------------------------------------------------------------------------
63 // wxScrollHelperEvtHandler: intercept the events from the window and forward
64 // them to wxScrollHelper
65 // ----------------------------------------------------------------------------
67 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
70 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
72 m_scrollHelper
= scrollHelper
;
75 virtual bool ProcessEvent(wxEvent
& event
);
77 void ResetDrawnFlag() { m_hasDrawnWindow
= FALSE
; }
80 wxScrollHelper
*m_scrollHelper
;
82 bool m_hasDrawnWindow
;
85 // ----------------------------------------------------------------------------
86 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
87 // a captured mouse is held outside the window
88 // ----------------------------------------------------------------------------
90 class wxAutoScrollTimer
: public wxTimer
93 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
94 wxEventType eventTypeToSend
,
97 virtual void Notify();
101 wxScrollHelper
*m_scrollHelper
;
102 wxEventType m_eventType
;
107 // ============================================================================
109 // ============================================================================
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
116 wxScrollHelper
*scroll
,
117 wxEventType eventTypeToSend
,
121 m_scrollHelper
= scroll
;
122 m_eventType
= eventTypeToSend
;
127 void wxAutoScrollTimer::Notify()
129 // only do all this as long as the window is capturing the mouse
130 if ( wxWindow::GetCapture() != m_win
)
134 else // we still capture the mouse, continue generating events
136 // first scroll the window if we are allowed to do it
137 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
138 event1
.SetEventObject(m_win
);
139 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
140 m_win
->GetEventHandler()->ProcessEvent(event1
) )
142 // and then send a pseudo mouse-move event to refresh the selection
143 wxMouseEvent
event2(wxEVT_MOTION
);
144 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
146 // the mouse event coordinates should be client, not screen as
147 // returned by wxGetMousePosition
148 wxWindow
*parentTop
= m_win
;
149 while ( parentTop
->GetParent() )
150 parentTop
= parentTop
->GetParent();
151 wxPoint ptOrig
= parentTop
->GetPosition();
152 event2
.m_x
-= ptOrig
.x
;
153 event2
.m_y
-= ptOrig
.y
;
155 event2
.SetEventObject(m_win
);
157 // FIXME: we don't fill in the other members - ok?
159 m_win
->GetEventHandler()->ProcessEvent(event2
);
161 else // can't scroll further, stop
168 // ----------------------------------------------------------------------------
169 // wxScrollHelperEvtHandler
170 // ----------------------------------------------------------------------------
172 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
174 wxEventType evType
= event
.GetEventType();
176 // always process the size events ourselves, even if the user code handles
177 // them as well, as we need to AdjustScrollbars()
178 if ( evType
== wxEVT_SIZE
)
180 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
183 // the explanation of wxEVT_PAINT processing hack: for historic reasons
184 // there are 2 ways to process this event in classes deriving from
185 // wxScrolledWindow. The user code may
187 // 1. override wxScrolledWindow::OnDraw(dc)
188 // 2. define its own OnPaint() handler
190 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
191 // always processes the draw event, so we can't just try the window
192 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
193 // (the latter would never be called in wxUniversal).
195 // So the solution is to have a flag telling us whether the user code drew
196 // anything in the window. We set it to true here but reset it to false in
197 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
198 // user code defined OnPaint() in the derived class)
199 m_hasDrawnWindow
= TRUE
;
201 if ( wxEvtHandler::ProcessEvent(event
) )
203 // normally, nothing more to do here - except if it was a paint event
204 // which wasn't really processed, then we'll try to call our
205 // OnDraw() below (from HandleOnPaint)
206 if ( !m_hasDrawnWindow
)
212 // reset the skipped flag to FALSE as it might have been set to TRUE in
213 // ProcessEvent() above
216 if ( evType
== wxEVT_PAINT
)
218 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
222 if ( evType
== wxEVT_SCROLLWIN_TOP
||
223 evType
== wxEVT_SCROLLWIN_BOTTOM
||
224 evType
== wxEVT_SCROLLWIN_LINEUP
||
225 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
226 evType
== wxEVT_SCROLLWIN_PAGEUP
||
227 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
228 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
229 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
231 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
232 return !event
.GetSkipped();
235 if ( evType
== wxEVT_ENTER_WINDOW
)
237 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
239 else if ( evType
== wxEVT_LEAVE_WINDOW
)
241 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
244 else if ( evType
== wxEVT_MOUSEWHEEL
)
246 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
248 #endif // wxUSE_MOUSEWHEEL
249 else if ( evType
== wxEVT_CHAR
)
251 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
252 return !event
.GetSkipped();
258 // ----------------------------------------------------------------------------
259 // wxScrollHelper construction
260 // ----------------------------------------------------------------------------
262 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
264 m_xScrollPixelsPerLine
=
265 m_yScrollPixelsPerLine
=
270 m_xScrollLinesPerPage
=
271 m_yScrollLinesPerPage
= 0;
273 m_xScrollingEnabled
=
274 m_yScrollingEnabled
= TRUE
;
283 m_targetWindow
= (wxWindow
*)NULL
;
285 m_timerAutoScroll
= (wxTimer
*)NULL
;
293 wxScrollHelper::~wxScrollHelper()
300 // ----------------------------------------------------------------------------
301 // setting scrolling parameters
302 // ----------------------------------------------------------------------------
304 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
314 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
317 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
318 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
*noUnitsX
) ||
320 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
321 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
*noUnitsY
) ||
322 (xPos
!= m_xScrollPosition
) ||
323 (yPos
!= m_yScrollPosition
)
326 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
327 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
328 m_xScrollPosition
= xPos
;
329 m_yScrollPosition
= yPos
;
330 m_xScrollLines
= noUnitsX
;
331 m_yScrollLines
= noUnitsY
;
334 // Sorry, some Motif-specific code to implement a backing pixmap
335 // for the wxRETAINED style. Implementing a backing store can't
336 // be entirely generic because it relies on the wxWindowDC implementation
337 // to duplicate X drawing calls for the backing pixmap.
339 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
341 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
343 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
344 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
345 if (m_targetWindow
->GetBackingPixmap() &&
346 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
347 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
349 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
350 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
353 if (!m_targetWindow
->GetBackingPixmap() &&
354 (noUnitsX
!= 0) && (noUnitsY
!= 0))
356 int depth
= wxDisplayDepth();
357 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
358 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
359 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
360 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
368 if (do_refresh
&& !noRefresh
)
369 m_targetWindow
->Refresh(TRUE
, GetRect());
372 m_targetWindow
->MacUpdateImmediately() ;
376 // ----------------------------------------------------------------------------
377 // target window handling
378 // ----------------------------------------------------------------------------
380 void wxScrollHelper::DeleteEvtHandler()
382 // FIXME: we should search for m_handler in the handler list
383 if ( m_targetWindow
)
385 m_targetWindow
->PopEventHandler(TRUE
/* Delete old event handler*/);
389 void wxScrollHelper::SetWindow(wxWindow
*win
)
391 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
395 DoSetTargetWindow(win
);
398 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
400 m_targetWindow
= target
;
402 // install the event handler which will intercept the events we're
404 m_handler
= new wxScrollHelperEvtHandler(this);
405 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
)
417 DoSetTargetWindow(target
);
420 wxWindow
*wxScrollHelper::GetTargetWindow() const
422 return m_targetWindow
;
425 // ----------------------------------------------------------------------------
426 // scrolling implementation itself
427 // ----------------------------------------------------------------------------
429 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
431 int nScrollInc
= CalcScrollInc(event
);
432 if ( nScrollInc
== 0 )
434 // can't scroll further
440 int orient
= event
.GetOrientation();
441 if (orient
== wxHORIZONTAL
)
443 m_xScrollPosition
+= nScrollInc
;
444 m_targetWindow
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
448 m_yScrollPosition
+= nScrollInc
;
449 m_targetWindow
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
452 bool needsRefresh
= FALSE
;
455 if (orient
== wxHORIZONTAL
)
457 if ( m_xScrollingEnabled
)
459 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
468 if ( m_yScrollingEnabled
)
470 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
480 m_targetWindow
->Refresh(TRUE
, GetRect());
484 m_targetWindow
->ScrollWindow(dx
, dy
, GetRect());
488 m_targetWindow
->MacUpdateImmediately() ;
492 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
494 int pos
= event
.GetPosition();
495 int orient
= event
.GetOrientation();
498 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
500 if (orient
== wxHORIZONTAL
)
501 nScrollInc
= - m_xScrollPosition
;
503 nScrollInc
= - m_yScrollPosition
;
505 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
507 if (orient
== wxHORIZONTAL
)
508 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
510 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
512 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
516 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
520 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
522 if (orient
== wxHORIZONTAL
)
523 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
525 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
527 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
529 if (orient
== wxHORIZONTAL
)
530 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
532 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
534 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
535 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
537 if (orient
== wxHORIZONTAL
)
538 nScrollInc
= pos
- m_xScrollPosition
;
540 nScrollInc
= pos
- m_yScrollPosition
;
543 if (orient
== wxHORIZONTAL
)
545 if (m_xScrollPixelsPerLine
> 0)
548 GetTargetSize(&w
, &h
);
550 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
551 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
555 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
556 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
557 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
558 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
561 m_targetWindow
->Refresh(TRUE
, GetRect());
565 if (m_yScrollPixelsPerLine
> 0)
568 GetTargetSize(&w
, &h
);
570 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
571 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
575 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
576 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
577 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
578 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
581 m_targetWindow
->Refresh(TRUE
, GetRect());
587 // Adjust the scrollbars - new version.
588 void wxScrollHelper::AdjustScrollbars()
591 m_targetWindow
->MacUpdateImmediately();
595 GetTargetSize(&w
, &h
);
597 int oldXScroll
= m_xScrollPosition
;
598 int oldYScroll
= m_yScrollPosition
;
600 if (m_xScrollLines
> 0)
602 // Calculate page size i.e. number of scroll units you get on the
603 // current client window
604 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
605 if (noPagePositions
< 1) noPagePositions
= 1;
606 if ( noPagePositions
> m_xScrollLines
)
607 noPagePositions
= m_xScrollLines
;
609 // Correct position if greater than extent of canvas minus
610 // the visible portion of it or if below zero
611 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
612 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
614 m_targetWindow
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
615 // The amount by which we scroll when paging
616 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
620 m_xScrollPosition
= 0;
621 m_targetWindow
->SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
624 if (m_yScrollLines
> 0)
626 // Calculate page size i.e. number of scroll units you get on the
627 // current client window
628 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
629 if (noPagePositions
< 1) noPagePositions
= 1;
630 if ( noPagePositions
> m_yScrollLines
)
631 noPagePositions
= m_yScrollLines
;
633 // Correct position if greater than extent of canvas minus
634 // the visible portion of it or if below zero
635 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
636 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
638 m_targetWindow
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
639 // The amount by which we scroll when paging
640 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
644 m_yScrollPosition
= 0;
645 m_targetWindow
->SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
648 if (oldXScroll
!= m_xScrollPosition
)
650 if (m_xScrollingEnabled
)
651 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0,
654 m_targetWindow
->Refresh(TRUE
, GetRect());
657 if (oldYScroll
!= m_yScrollPosition
)
659 if (m_yScrollingEnabled
)
660 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
663 m_targetWindow
->Refresh(TRUE
, GetRect());
667 m_targetWindow
->MacUpdateImmediately();
671 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
673 wxPoint pt
= dc
.GetDeviceOrigin();
674 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
675 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
676 dc
.SetUserScale( m_scaleX
, m_scaleY
);
679 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
682 *x_unit
= m_xScrollPixelsPerLine
;
684 *y_unit
= m_yScrollPixelsPerLine
;
687 int wxScrollHelper::GetScrollPageSize(int orient
) const
689 if ( orient
== wxHORIZONTAL
)
690 return m_xScrollLinesPerPage
;
692 return m_yScrollLinesPerPage
;
695 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
697 if ( orient
== wxHORIZONTAL
)
698 m_xScrollLinesPerPage
= pageSize
;
700 m_yScrollLinesPerPage
= pageSize
;
704 * Scroll to given position (scroll position, not pixel position)
706 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
711 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
712 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
715 m_targetWindow
->MacUpdateImmediately();
719 GetTargetSize(&w
, &h
);
721 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
723 int old_x
= m_xScrollPosition
;
724 m_xScrollPosition
= x_pos
;
726 // Calculate page size i.e. number of scroll units you get on the
727 // current client window
728 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
729 if (noPagePositions
< 1) noPagePositions
= 1;
731 // Correct position if greater than extent of canvas minus
732 // the visible portion of it or if below zero
733 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
734 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
736 if (old_x
!= m_xScrollPosition
) {
737 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
738 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
742 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
744 int old_y
= m_yScrollPosition
;
745 m_yScrollPosition
= y_pos
;
747 // Calculate page size i.e. number of scroll units you get on the
748 // current client window
749 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
750 if (noPagePositions
< 1) noPagePositions
= 1;
752 // Correct position if greater than extent of canvas minus
753 // the visible portion of it or if below zero
754 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
755 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
757 if (old_y
!= m_yScrollPosition
) {
758 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
759 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
765 m_targetWindow
->MacUpdateImmediately();
770 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
772 m_xScrollingEnabled
= x_scroll
;
773 m_yScrollingEnabled
= y_scroll
;
776 void wxScrollHelper::GetVirtualSize (int *x
, int *y
) const
779 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
781 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
784 // Where the current view starts from
785 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
788 *x
= m_xScrollPosition
;
790 *y
= m_yScrollPosition
;
793 void wxScrollHelper::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
796 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
798 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
801 void wxScrollHelper::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
804 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
806 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
809 // ----------------------------------------------------------------------------
811 // ----------------------------------------------------------------------------
813 // Default OnSize resets scrollbars, if any
814 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
816 #if wxUSE_CONSTRAINTS
817 if ( m_targetWindow
->GetAutoLayout() )
818 m_targetWindow
->Layout();
824 // This calls OnDraw, having adjusted the origin according to the current
826 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
828 wxPaintDC
dc(m_targetWindow
);
834 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
835 // compatibility here - if we used OnKeyDown(), the programs which process
836 // arrows themselves in their OnChar() would never get the message and like
837 // this they always have the priority
838 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
840 int stx
, sty
, // view origin
841 szx
, szy
, // view size (total)
842 clix
, cliy
; // view size (on screen)
844 GetViewStart(&stx
, &sty
);
845 GetTargetSize(&clix
, &cliy
);
846 GetVirtualSize(&szx
, &szy
);
848 if( m_xScrollPixelsPerLine
)
850 clix
/= m_xScrollPixelsPerLine
;
851 szx
/= m_xScrollPixelsPerLine
;
858 if( m_yScrollPixelsPerLine
)
860 cliy
/= m_yScrollPixelsPerLine
;
861 szy
/= m_yScrollPixelsPerLine
;
869 int xScrollOld
= m_xScrollPosition
,
870 yScrollOld
= m_yScrollPosition
;
873 switch ( event
.KeyCode() )
877 dsty
= sty
- (5 * cliy
/ 6);
878 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
883 Scroll(-1, sty
+ (5 * cliy
/ 6));
887 Scroll(0, event
.ControlDown() ? 0 : -1);
891 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
915 if ( m_xScrollPosition
!= xScrollOld
)
917 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
919 event
.SetEventObject(m_win
);
920 m_win
->GetEventHandler()->ProcessEvent(event
);
923 if ( m_yScrollPosition
!= yScrollOld
)
925 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
927 event
.SetEventObject(m_win
);
928 m_win
->GetEventHandler()->ProcessEvent(event
);
932 // ----------------------------------------------------------------------------
933 // autoscroll stuff: these functions deal with sending fake scroll events when
934 // a captured mouse is being held outside the window
935 // ----------------------------------------------------------------------------
937 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
939 // only send the event if the window is scrollable in this direction
940 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
941 return win
->HasScrollbar(event
.GetOrientation());
944 void wxScrollHelper::StopAutoScrolling()
946 if ( m_timerAutoScroll
)
948 delete m_timerAutoScroll
;
949 m_timerAutoScroll
= (wxTimer
*)NULL
;
953 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
960 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
962 // don't prevent the usual processing of the event from taking place
965 // when a captured mouse leave a scrolled window we start generate
966 // scrolling events to allow, for example, extending selection beyond the
967 // visible area in some controls
968 if ( wxWindow::GetCapture() == m_targetWindow
)
970 // where is the mouse leaving?
972 wxPoint pt
= event
.GetPosition();
975 orient
= wxHORIZONTAL
;
983 else // we're lower or to the right of the window
985 wxSize size
= m_targetWindow
->GetClientSize();
988 orient
= wxHORIZONTAL
;
989 pos
= m_xScrollLines
;
991 else if ( pt
.y
> size
.y
)
994 pos
= m_yScrollLines
;
996 else // this should be impossible
998 // but seems to happen sometimes under wxMSW - maybe it's a bug
999 // there but for now just ignore it
1001 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1007 // only start the auto scroll timer if the window can be scrolled in
1009 if ( !m_targetWindow
->HasScrollbar(orient
) )
1012 delete m_timerAutoScroll
;
1013 m_timerAutoScroll
= new wxAutoScrollTimer
1015 m_targetWindow
, this,
1016 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1017 : wxEVT_SCROLLWIN_LINEDOWN
,
1021 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1025 #if wxUSE_MOUSEWHEEL
1027 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1029 m_wheelRotation
+= event
.GetWheelRotation();
1030 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1031 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1035 lines
*= event
.GetLinesPerAction();
1037 wxScrollWinEvent newEvent
;
1039 newEvent
.SetPosition(0);
1040 newEvent
.SetOrientation(wxVERTICAL
);
1041 newEvent
.m_eventObject
= m_win
;
1043 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEUP
;
1045 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
1047 int times
= abs(lines
);
1048 for (; times
> 0; times
--)
1049 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1053 // GetViewStart(&vsx, &vsy);
1054 // Scroll(-1, vsy - lines);
1058 #endif // wxUSE_MOUSEWHEEL
1060 // ----------------------------------------------------------------------------
1061 // wxGenericScrolledWindow implementation
1062 // ----------------------------------------------------------------------------
1064 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1066 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1067 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1070 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1075 const wxString
& name
)
1077 m_targetWindow
= this;
1079 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1082 // we need to process arrows ourselves for scrolling
1083 m_lDlgCode
|= DLGC_WANTARROWS
;
1089 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1093 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1095 // the user code didn't really draw the window if we got here, so set this
1096 // flag to try to call OnDraw() later
1097 m_handler
->ResetDrawnFlag();
1102 #if WXWIN_COMPATIBILITY
1104 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
1106 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
1107 *y_page
= GetScrollPageSize(wxVERTICAL
);
1110 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
1113 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
1115 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
1118 #endif // WXWIN_COMPATIBILITY