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 wxScrollHelperEvtHandler
: public wxEvtHandler
70 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
72 m_scrollHelper
= scrollHelper
;
75 virtual bool ProcessEvent(wxEvent
& event
);
78 wxScrollHelper
*m_scrollHelper
;
81 // ----------------------------------------------------------------------------
82 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
83 // a captured mouse is held outside the window
84 // ----------------------------------------------------------------------------
86 class wxAutoScrollTimer
: public wxTimer
89 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
90 wxEventType eventTypeToSend
,
93 virtual void Notify();
97 wxScrollHelper
*m_scrollHelper
;
98 wxEventType m_eventType
;
103 // ============================================================================
105 // ============================================================================
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
112 wxScrollHelper
*scroll
,
113 wxEventType eventTypeToSend
,
117 m_scrollHelper
= scroll
;
118 m_eventType
= eventTypeToSend
;
123 void wxAutoScrollTimer::Notify()
125 // only do all this as long as the window is capturing the mouse
126 if ( wxWindow::GetCapture() != m_win
)
130 else // we still capture the mouse, continue generating events
132 // first scroll the window if we are allowed to do it
133 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
134 event1
.SetEventObject(m_win
);
135 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
136 m_win
->GetEventHandler()->ProcessEvent(event1
) )
138 // and then send a pseudo mouse-move event to refresh the selection
139 wxMouseEvent
event2(wxEVT_MOTION
);
140 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
142 // the mouse event coordinates should be client, not screen as
143 // returned by wxGetMousePosition
144 wxWindow
*parentTop
= m_win
;
145 while ( parentTop
->GetParent() )
146 parentTop
= parentTop
->GetParent();
147 wxPoint ptOrig
= parentTop
->GetPosition();
148 event2
.m_x
-= ptOrig
.x
;
149 event2
.m_y
-= ptOrig
.y
;
151 event2
.SetEventObject(m_win
);
153 // FIXME: we don't fill in the other members - ok?
155 m_win
->GetEventHandler()->ProcessEvent(event2
);
157 else // can't scroll further, stop
164 // ----------------------------------------------------------------------------
165 // wxScrollHelperEvtHandler
166 // ----------------------------------------------------------------------------
168 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
170 wxEventType evType
= event
.GetEventType();
172 if ( evType
== wxEVT_SIZE
)
174 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
177 if ( wxEvtHandler::ProcessEvent(event
) )
180 // reset the skipped flag to FALSE as it might have been set to TRUE in
181 // ProcessEvent() above
184 if ( evType
== wxEVT_PAINT
)
186 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
190 if ( evType
== wxEVT_SCROLLWIN_TOP
||
191 evType
== wxEVT_SCROLLWIN_BOTTOM
||
192 evType
== wxEVT_SCROLLWIN_LINEUP
||
193 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
194 evType
== wxEVT_SCROLLWIN_PAGEUP
||
195 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
196 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
197 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
199 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
200 return !event
.GetSkipped();
203 if ( evType
== wxEVT_ENTER_WINDOW
)
205 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
207 else if ( evType
== wxEVT_LEAVE_WINDOW
)
209 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
212 else if ( evType
== wxEVT_MOUSEWHEEL
)
214 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
216 #endif // wxUSE_MOUSEWHEEL
217 else if ( evType
== wxEVT_CHAR
)
219 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
220 return !event
.GetSkipped();
226 // ----------------------------------------------------------------------------
227 // wxScrollHelper construction
228 // ----------------------------------------------------------------------------
230 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
232 m_xScrollPixelsPerLine
=
233 m_yScrollPixelsPerLine
=
238 m_xScrollLinesPerPage
=
239 m_yScrollLinesPerPage
= 0;
241 m_xScrollingEnabled
=
242 m_yScrollingEnabled
= TRUE
;
251 m_targetWindow
= (wxWindow
*)NULL
;
253 m_timerAutoScroll
= (wxTimer
*)NULL
;
259 void wxScrollHelper::SetWindow(wxWindow
*win
)
261 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
263 m_targetWindow
= m_win
= win
;
265 // install the event handler which will intercept the events we're
267 m_win
->PushEventHandler(new wxScrollHelperEvtHandler(this));
270 wxScrollHelper::~wxScrollHelper()
274 if ( m_targetWindow
)
275 m_targetWindow
->PopEventHandler(TRUE
/* do delete it */);
276 if ( m_win
&& m_win
!= m_targetWindow
)
277 m_win
->PopEventHandler(TRUE
/* do delete it */);
280 // ----------------------------------------------------------------------------
281 // setting scrolling parameters
282 // ----------------------------------------------------------------------------
284 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
294 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
297 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
298 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
*noUnitsX
) ||
300 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
301 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
*noUnitsY
) ||
302 (xPos
!= m_xScrollPosition
) ||
303 (yPos
!= m_yScrollPosition
)
306 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
307 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
308 m_xScrollPosition
= xPos
;
309 m_yScrollPosition
= yPos
;
310 m_xScrollLines
= noUnitsX
;
311 m_yScrollLines
= noUnitsY
;
314 // Sorry, some Motif-specific code to implement a backing pixmap
315 // for the wxRETAINED style. Implementing a backing store can't
316 // be entirely generic because it relies on the wxWindowDC implementation
317 // to duplicate X drawing calls for the backing pixmap.
319 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
321 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
323 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
324 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
325 if (m_targetWindow
->GetBackingPixmap() &&
326 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
327 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
329 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
330 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
333 if (!m_targetWindow
->GetBackingPixmap() &&
334 (noUnitsX
!= 0) && (noUnitsY
!= 0))
336 int depth
= wxDisplayDepth();
337 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
338 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
339 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
340 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
348 if (do_refresh
&& !noRefresh
)
349 m_targetWindow
->Refresh(TRUE
, GetRect());
352 m_targetWindow
->MacUpdateImmediately() ;
356 // ----------------------------------------------------------------------------
357 // target window handling
358 // ----------------------------------------------------------------------------
360 void wxScrollHelper::SetTargetWindow( wxWindow
*target
)
362 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
363 // FIXME: There is a potential problem with this way of deleting
364 // event handlers, basically you can not be sure that you delete
365 // the event handler that was create by this wxScrollHelper.
366 // Remove the old event handler from the previous target scroll window.
367 if (m_targetWindow
&& m_targetWindow
!= m_win
)
368 m_targetWindow
->PopEventHandler(TRUE
/* Delete old event handler*/);
369 m_targetWindow
= target
;
370 // Install a new event handler, which will intercept the events we're
371 // interested in from the target scroll window.
372 if (m_targetWindow
!= m_win
)
373 m_targetWindow
->PushEventHandler(new wxScrollHelperEvtHandler(this));
376 wxWindow
*wxScrollHelper::GetTargetWindow() const
378 return m_targetWindow
;
381 // ----------------------------------------------------------------------------
382 // scrolling implementation itself
383 // ----------------------------------------------------------------------------
385 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
387 int nScrollInc
= CalcScrollInc(event
);
388 if ( nScrollInc
== 0 )
390 // can't scroll further
396 int orient
= event
.GetOrientation();
397 if (orient
== wxHORIZONTAL
)
399 m_xScrollPosition
+= nScrollInc
;
400 m_targetWindow
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
404 m_yScrollPosition
+= nScrollInc
;
405 m_targetWindow
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
408 bool needsRefresh
= FALSE
;
411 if (orient
== wxHORIZONTAL
)
413 if ( m_xScrollingEnabled
)
415 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
424 if ( m_yScrollingEnabled
)
426 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
436 m_targetWindow
->Refresh(TRUE
, GetRect());
440 m_targetWindow
->ScrollWindow(dx
, dy
, GetRect());
444 m_targetWindow
->MacUpdateImmediately() ;
448 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
450 int pos
= event
.GetPosition();
451 int orient
= event
.GetOrientation();
454 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
456 if (orient
== wxHORIZONTAL
)
457 nScrollInc
= - m_xScrollPosition
;
459 nScrollInc
= - m_yScrollPosition
;
461 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
463 if (orient
== wxHORIZONTAL
)
464 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
466 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
468 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
472 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
476 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
478 if (orient
== wxHORIZONTAL
)
479 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
481 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
483 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
485 if (orient
== wxHORIZONTAL
)
486 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
488 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
490 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
491 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
493 if (orient
== wxHORIZONTAL
)
494 nScrollInc
= pos
- m_xScrollPosition
;
496 nScrollInc
= pos
- m_yScrollPosition
;
499 if (orient
== wxHORIZONTAL
)
501 if (m_xScrollPixelsPerLine
> 0)
504 GetTargetSize(&w
, &h
);
506 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
507 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
511 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
512 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
513 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
514 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
517 m_targetWindow
->Refresh(TRUE
, GetRect());
521 if (m_yScrollPixelsPerLine
> 0)
524 GetTargetSize(&w
, &h
);
526 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
527 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
531 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
532 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
533 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
534 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
537 m_targetWindow
->Refresh(TRUE
, GetRect());
543 // Adjust the scrollbars - new version.
544 void wxScrollHelper::AdjustScrollbars()
547 m_targetWindow
->MacUpdateImmediately();
551 GetTargetSize(&w
, &h
);
553 int oldXScroll
= m_xScrollPosition
;
554 int oldYScroll
= m_yScrollPosition
;
556 if (m_xScrollLines
> 0)
558 // Calculate page size i.e. number of scroll units you get on the
559 // current client window
560 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
561 if (noPagePositions
< 1) noPagePositions
= 1;
562 if ( noPagePositions
> m_xScrollLines
)
563 noPagePositions
= m_xScrollLines
;
565 // Correct position if greater than extent of canvas minus
566 // the visible portion of it or if below zero
567 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
568 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
570 m_targetWindow
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
571 // The amount by which we scroll when paging
572 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
576 m_xScrollPosition
= 0;
577 m_targetWindow
->SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
580 if (m_yScrollLines
> 0)
582 // Calculate page size i.e. number of scroll units you get on the
583 // current client window
584 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
585 if (noPagePositions
< 1) noPagePositions
= 1;
586 if ( noPagePositions
> m_yScrollLines
)
587 noPagePositions
= m_yScrollLines
;
589 // Correct position if greater than extent of canvas minus
590 // the visible portion of it or if below zero
591 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
592 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
594 m_targetWindow
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
595 // The amount by which we scroll when paging
596 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
600 m_yScrollPosition
= 0;
601 m_targetWindow
->SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
604 if (oldXScroll
!= m_xScrollPosition
)
606 if (m_xScrollingEnabled
)
607 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0,
610 m_targetWindow
->Refresh(TRUE
, GetRect());
613 if (oldYScroll
!= m_yScrollPosition
)
615 if (m_yScrollingEnabled
)
616 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
619 m_targetWindow
->Refresh(TRUE
, GetRect());
623 m_targetWindow
->MacUpdateImmediately();
627 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
629 wxPoint pt
= dc
.GetDeviceOrigin();
630 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
631 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
632 dc
.SetUserScale( m_scaleX
, m_scaleY
);
635 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
638 *x_unit
= m_xScrollPixelsPerLine
;
640 *y_unit
= m_yScrollPixelsPerLine
;
643 int wxScrollHelper::GetScrollPageSize(int orient
) const
645 if ( orient
== wxHORIZONTAL
)
646 return m_xScrollLinesPerPage
;
648 return m_yScrollLinesPerPage
;
651 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
653 if ( orient
== wxHORIZONTAL
)
654 m_xScrollLinesPerPage
= pageSize
;
656 m_yScrollLinesPerPage
= pageSize
;
660 * Scroll to given position (scroll position, not pixel position)
662 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
667 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
668 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
671 m_targetWindow
->MacUpdateImmediately();
675 GetTargetSize(&w
, &h
);
677 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
679 int old_x
= m_xScrollPosition
;
680 m_xScrollPosition
= x_pos
;
682 // Calculate page size i.e. number of scroll units you get on the
683 // current client window
684 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
685 if (noPagePositions
< 1) noPagePositions
= 1;
687 // Correct position if greater than extent of canvas minus
688 // the visible portion of it or if below zero
689 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
690 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
692 if (old_x
!= m_xScrollPosition
) {
693 m_targetWindow
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
694 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
698 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
700 int old_y
= m_yScrollPosition
;
701 m_yScrollPosition
= y_pos
;
703 // Calculate page size i.e. number of scroll units you get on the
704 // current client window
705 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
706 if (noPagePositions
< 1) noPagePositions
= 1;
708 // Correct position if greater than extent of canvas minus
709 // the visible portion of it or if below zero
710 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
711 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
713 if (old_y
!= m_yScrollPosition
) {
714 m_targetWindow
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
715 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
721 m_targetWindow
->MacUpdateImmediately();
726 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
728 m_xScrollingEnabled
= x_scroll
;
729 m_yScrollingEnabled
= y_scroll
;
732 void wxScrollHelper::GetVirtualSize (int *x
, int *y
) const
735 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
737 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
740 // Where the current view starts from
741 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
744 *x
= m_xScrollPosition
;
746 *y
= m_yScrollPosition
;
749 void wxScrollHelper::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
752 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
754 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
757 void wxScrollHelper::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
760 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
762 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
765 // ----------------------------------------------------------------------------
767 // ----------------------------------------------------------------------------
769 // Default OnSize resets scrollbars, if any
770 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
772 #if wxUSE_CONSTRAINTS
773 if ( m_targetWindow
->GetAutoLayout() )
774 m_targetWindow
->Layout();
780 // This calls OnDraw, having adjusted the origin according to the current
782 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
784 wxPaintDC
dc(m_targetWindow
);
790 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
791 // compatibility here - if we used OnKeyDown(), the programs which process
792 // arrows themselves in their OnChar() would never get the message and like
793 // this they always have the priority
794 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
796 int stx
, sty
, // view origin
797 szx
, szy
, // view size (total)
798 clix
, cliy
; // view size (on screen)
800 GetViewStart(&stx
, &sty
);
801 GetTargetSize(&clix
, &cliy
);
802 GetVirtualSize(&szx
, &szy
);
804 if( m_xScrollPixelsPerLine
)
806 clix
/= m_xScrollPixelsPerLine
;
807 szx
/= m_xScrollPixelsPerLine
;
814 if( m_yScrollPixelsPerLine
)
816 cliy
/= m_yScrollPixelsPerLine
;
817 szy
/= m_yScrollPixelsPerLine
;
825 int xScrollOld
= m_xScrollPosition
,
826 yScrollOld
= m_yScrollPosition
;
829 switch ( event
.KeyCode() )
833 dsty
= sty
- (5 * cliy
/ 6);
834 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
839 Scroll(-1, sty
+ (5 * cliy
/ 6));
843 Scroll(0, event
.ControlDown() ? 0 : -1);
847 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
871 if ( m_xScrollPosition
!= xScrollOld
)
873 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
875 event
.SetEventObject(m_win
);
876 m_win
->GetEventHandler()->ProcessEvent(event
);
879 if ( m_yScrollPosition
!= yScrollOld
)
881 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
883 event
.SetEventObject(m_win
);
884 m_win
->GetEventHandler()->ProcessEvent(event
);
888 // ----------------------------------------------------------------------------
889 // autoscroll stuff: these functions deal with sending fake scroll events when
890 // a captured mouse is being held outside the window
891 // ----------------------------------------------------------------------------
893 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
895 // only send the event if the window is scrollable in this direction
896 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
897 return win
->HasScrollbar(event
.GetOrientation());
900 void wxScrollHelper::StopAutoScrolling()
902 if ( m_timerAutoScroll
)
904 delete m_timerAutoScroll
;
905 m_timerAutoScroll
= (wxTimer
*)NULL
;
909 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
916 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
918 // don't prevent the usual processing of the event from taking place
921 // when a captured mouse leave a scrolled window we start generate
922 // scrolling events to allow, for example, extending selection beyond the
923 // visible area in some controls
924 if ( wxWindow::GetCapture() == m_targetWindow
)
926 // where is the mouse leaving?
928 wxPoint pt
= event
.GetPosition();
931 orient
= wxHORIZONTAL
;
939 else // we're lower or to the right of the window
941 wxSize size
= m_targetWindow
->GetClientSize();
944 orient
= wxHORIZONTAL
;
945 pos
= m_xScrollLines
;
947 else if ( pt
.y
> size
.y
)
950 pos
= m_yScrollLines
;
952 else // this should be impossible
954 // but seems to happen sometimes under wxMSW - maybe it's a bug
955 // there but for now just ignore it
957 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
963 // only start the auto scroll timer if the window can be scrolled in
965 if ( !m_targetWindow
->HasScrollbar(orient
) )
968 delete m_timerAutoScroll
;
969 m_timerAutoScroll
= new wxAutoScrollTimer
971 m_targetWindow
, this,
972 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
973 : wxEVT_SCROLLWIN_LINEDOWN
,
977 m_timerAutoScroll
->Start(50); // FIXME: make configurable
983 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
985 m_wheelRotation
+= event
.GetWheelRotation();
986 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
987 m_wheelRotation
-= lines
* event
.GetWheelDelta();
991 lines
*= event
.GetLinesPerAction();
993 wxScrollWinEvent newEvent
;
995 newEvent
.SetPosition(0);
996 newEvent
.SetOrientation(wxVERTICAL
);
997 newEvent
.m_eventObject
= m_win
;
999 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEUP
;
1001 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
1003 int times
= abs(lines
);
1004 for (; times
> 0; times
--)
1005 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1009 // GetViewStart(&vsx, &vsy);
1010 // Scroll(-1, vsy - lines);
1014 #endif // wxUSE_MOUSEWHEEL
1016 // ----------------------------------------------------------------------------
1017 // wxGenericScrolledWindow implementation
1018 // ----------------------------------------------------------------------------
1020 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1022 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1027 const wxString
& name
)
1029 m_targetWindow
= this;
1031 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1034 // we need to process arrows ourselves for scrolling
1035 m_lDlgCode
|= DLGC_WANTARROWS
;
1041 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1045 #if WXWIN_COMPATIBILITY
1046 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
1048 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
1049 *y_page
= GetScrollPageSize(wxVERTICAL
);
1052 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
1055 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
1057 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
1059 #endif // WXWIN_COMPATIBILITY