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 // the explanation of wxEVT_PAINT processing hack: for historic reasons
177 // there are 2 ways to process this event in classes deriving from
178 // wxScrolledWindow. The user code may
180 // 1. override wxScrolledWindow::OnDraw(dc)
181 // 2. define its own OnPaint() handler
183 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
184 // always processes the draw event, so we can't just try the window
185 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
186 // (the latter would never be called in wxUniversal).
188 // So the solution is to have a flag telling us whether the user code drew
189 // anything in the window. We set it to true here but reset it to false in
190 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
191 // user code defined OnPaint() in the derived class)
192 m_hasDrawnWindow
= TRUE
;
194 // pass it on to the real handler
195 bool processed
= wxEvtHandler::ProcessEvent(event
);
197 // always process the size events ourselves, even if the user code handles
198 // them as well, as we need to AdjustScrollbars()
200 // NB: it is important to do it after processing the event in the normal
201 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
202 // scrollbar[s] (dis)appear and it should be seen by the user code
204 if ( evType
== wxEVT_SIZE
)
206 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
213 // normally, nothing more to do here - except if it was a paint event
214 // which wasn't really processed, then we'll try to call our
215 // OnDraw() below (from HandleOnPaint)
216 if ( m_hasDrawnWindow
)
222 // reset the skipped flag to FALSE as it might have been set to TRUE in
223 // ProcessEvent() above
226 if ( evType
== wxEVT_PAINT
)
228 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
232 if ( evType
== wxEVT_SCROLLWIN_TOP
||
233 evType
== wxEVT_SCROLLWIN_BOTTOM
||
234 evType
== wxEVT_SCROLLWIN_LINEUP
||
235 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
236 evType
== wxEVT_SCROLLWIN_PAGEUP
||
237 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
238 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
239 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
241 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
242 return !event
.GetSkipped();
245 if ( evType
== wxEVT_ENTER_WINDOW
)
247 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
249 else if ( evType
== wxEVT_LEAVE_WINDOW
)
251 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
254 else if ( evType
== wxEVT_MOUSEWHEEL
)
256 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
258 #endif // wxUSE_MOUSEWHEEL
259 else if ( evType
== wxEVT_CHAR
)
261 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
262 return !event
.GetSkipped();
268 // ----------------------------------------------------------------------------
269 // wxScrollHelper construction
270 // ----------------------------------------------------------------------------
272 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
274 m_xScrollPixelsPerLine
=
275 m_yScrollPixelsPerLine
=
280 m_xScrollLinesPerPage
=
281 m_yScrollLinesPerPage
= 0;
283 m_xScrollingEnabled
=
284 m_yScrollingEnabled
= TRUE
;
293 m_targetWindow
= (wxWindow
*)NULL
;
295 m_timerAutoScroll
= (wxTimer
*)NULL
;
303 wxScrollHelper::~wxScrollHelper()
310 // ----------------------------------------------------------------------------
311 // setting scrolling parameters
312 // ----------------------------------------------------------------------------
314 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
324 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
327 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
328 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
*noUnitsX
) ||
330 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
331 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
*noUnitsY
) ||
332 (xPos
!= m_xScrollPosition
) ||
333 (yPos
!= m_yScrollPosition
)
336 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
337 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
338 m_xScrollPosition
= xPos
;
339 m_yScrollPosition
= yPos
;
340 m_xScrollLines
= noUnitsX
;
341 m_yScrollLines
= noUnitsY
;
344 // Sorry, some Motif-specific code to implement a backing pixmap
345 // for the wxRETAINED style. Implementing a backing store can't
346 // be entirely generic because it relies on the wxWindowDC implementation
347 // to duplicate X drawing calls for the backing pixmap.
349 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
351 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
353 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
354 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
355 if (m_targetWindow
->GetBackingPixmap() &&
356 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
357 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
359 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
360 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
363 if (!m_targetWindow
->GetBackingPixmap() &&
364 (noUnitsX
!= 0) && (noUnitsY
!= 0))
366 int depth
= wxDisplayDepth();
367 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
368 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
369 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
370 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
378 if (do_refresh
&& !noRefresh
)
379 m_targetWindow
->Refresh(TRUE
, GetRect());
382 m_targetWindow
->MacUpdateImmediately() ;
386 // ----------------------------------------------------------------------------
387 // [target] window handling
388 // ----------------------------------------------------------------------------
390 void wxScrollHelper::DeleteEvtHandler()
392 // search for m_handler in the handler list
393 if ( m_win
&& m_handler
)
395 wxEvtHandler
*handlerPrev
= NULL
,
396 *handler
= m_win
->GetEventHandler();
399 if ( handler
== m_handler
)
401 wxEvtHandler
*handlerNext
= handler
->GetNextHandler();
404 handlerPrev
->SetNextHandler(handlerNext
);
408 m_win
->SetEventHandler(handlerNext
);
411 handler
->SetNextHandler(NULL
);
418 handlerPrev
= handler
;
419 handler
= handler
->GetNextHandler();
422 wxFAIL_MSG( _T("where has our event handler gone?") );
426 void wxScrollHelper::SetWindow(wxWindow
*win
)
428 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
432 // by default, the associated window is also the target window
433 DoSetTargetWindow(win
);
436 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
438 m_targetWindow
= target
;
440 // install the event handler which will intercept the events we're
441 // interested in (but only do it for our real window, not the target window
442 // which we scroll - we don't need to hijack its events)
443 if ( m_targetWindow
== m_win
)
445 // if we already have a handler, delete it first
448 m_handler
= new wxScrollHelperEvtHandler(this);
449 m_targetWindow
->PushEventHandler(m_handler
);
453 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
455 wxCHECK_RET( target
, wxT("target window must not be NULL") );
457 if ( target
== m_targetWindow
)
460 DoSetTargetWindow(target
);
463 wxWindow
*wxScrollHelper::GetTargetWindow() const
465 return m_targetWindow
;
468 // ----------------------------------------------------------------------------
469 // scrolling implementation itself
470 // ----------------------------------------------------------------------------
472 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
474 int nScrollInc
= CalcScrollInc(event
);
475 if ( nScrollInc
== 0 )
477 // can't scroll further
483 int orient
= event
.GetOrientation();
484 if (orient
== wxHORIZONTAL
)
486 m_xScrollPosition
+= nScrollInc
;
487 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
491 m_yScrollPosition
+= nScrollInc
;
492 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
495 bool needsRefresh
= FALSE
;
498 if (orient
== wxHORIZONTAL
)
500 if ( m_xScrollingEnabled
)
502 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
511 if ( m_yScrollingEnabled
)
513 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
523 m_targetWindow
->Refresh(TRUE
, GetRect());
527 m_targetWindow
->ScrollWindow(dx
, dy
, GetRect());
531 m_targetWindow
->MacUpdateImmediately() ;
535 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
537 int pos
= event
.GetPosition();
538 int orient
= event
.GetOrientation();
541 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
543 if (orient
== wxHORIZONTAL
)
544 nScrollInc
= - m_xScrollPosition
;
546 nScrollInc
= - m_yScrollPosition
;
548 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
550 if (orient
== wxHORIZONTAL
)
551 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
553 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
555 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
559 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
563 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
565 if (orient
== wxHORIZONTAL
)
566 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
568 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
570 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
572 if (orient
== wxHORIZONTAL
)
573 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
575 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
577 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
578 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
580 if (orient
== wxHORIZONTAL
)
581 nScrollInc
= pos
- m_xScrollPosition
;
583 nScrollInc
= pos
- m_yScrollPosition
;
586 if (orient
== wxHORIZONTAL
)
588 if (m_xScrollPixelsPerLine
> 0)
591 GetTargetSize(&w
, &h
);
593 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
594 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
598 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
599 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
600 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
601 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
604 m_targetWindow
->Refresh(TRUE
, GetRect());
608 if (m_yScrollPixelsPerLine
> 0)
611 GetTargetSize(&w
, &h
);
613 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
614 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
618 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
619 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
620 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
621 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
624 m_targetWindow
->Refresh(TRUE
, GetRect());
630 // Adjust the scrollbars - new version.
631 void wxScrollHelper::AdjustScrollbars()
634 m_targetWindow
->MacUpdateImmediately();
638 GetTargetSize(&w
, &h
);
640 int oldXScroll
= m_xScrollPosition
;
641 int oldYScroll
= m_yScrollPosition
;
643 if (m_xScrollLines
> 0)
645 // Calculate page size i.e. number of scroll units you get on the
646 // current client window
647 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
648 if (noPagePositions
< 1) noPagePositions
= 1;
649 if ( noPagePositions
> m_xScrollLines
)
650 noPagePositions
= m_xScrollLines
;
652 // Correct position if greater than extent of canvas minus
653 // the visible portion of it or if below zero
654 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
655 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
657 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
658 // The amount by which we scroll when paging
659 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
663 m_xScrollPosition
= 0;
664 m_win
->SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
667 if (m_yScrollLines
> 0)
669 // Calculate page size i.e. number of scroll units you get on the
670 // current client window
671 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
672 if (noPagePositions
< 1) noPagePositions
= 1;
673 if ( noPagePositions
> m_yScrollLines
)
674 noPagePositions
= m_yScrollLines
;
676 // Correct position if greater than extent of canvas minus
677 // the visible portion of it or if below zero
678 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
679 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
681 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
682 // The amount by which we scroll when paging
683 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
687 m_yScrollPosition
= 0;
688 m_win
->SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
691 if (oldXScroll
!= m_xScrollPosition
)
693 if (m_xScrollingEnabled
)
694 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0,
697 m_targetWindow
->Refresh(TRUE
, GetRect());
700 if (oldYScroll
!= m_yScrollPosition
)
702 if (m_yScrollingEnabled
)
703 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
706 m_targetWindow
->Refresh(TRUE
, GetRect());
710 m_targetWindow
->MacUpdateImmediately();
714 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
716 wxPoint pt
= dc
.GetDeviceOrigin();
717 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
718 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
719 dc
.SetUserScale( m_scaleX
, m_scaleY
);
722 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
725 *x_unit
= m_xScrollPixelsPerLine
;
727 *y_unit
= m_yScrollPixelsPerLine
;
730 int wxScrollHelper::GetScrollPageSize(int orient
) const
732 if ( orient
== wxHORIZONTAL
)
733 return m_xScrollLinesPerPage
;
735 return m_yScrollLinesPerPage
;
738 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
740 if ( orient
== wxHORIZONTAL
)
741 m_xScrollLinesPerPage
= pageSize
;
743 m_yScrollLinesPerPage
= pageSize
;
747 * Scroll to given position (scroll position, not pixel position)
749 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
754 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
755 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
758 m_targetWindow
->MacUpdateImmediately();
762 GetTargetSize(&w
, &h
);
764 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
766 int old_x
= m_xScrollPosition
;
767 m_xScrollPosition
= x_pos
;
769 // Calculate page size i.e. number of scroll units you get on the
770 // current client window
771 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
772 if (noPagePositions
< 1) noPagePositions
= 1;
774 // Correct position if greater than extent of canvas minus
775 // the visible portion of it or if below zero
776 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
777 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
779 if (old_x
!= m_xScrollPosition
) {
780 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
781 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
785 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
787 int old_y
= m_yScrollPosition
;
788 m_yScrollPosition
= y_pos
;
790 // Calculate page size i.e. number of scroll units you get on the
791 // current client window
792 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
793 if (noPagePositions
< 1) noPagePositions
= 1;
795 // Correct position if greater than extent of canvas minus
796 // the visible portion of it or if below zero
797 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
798 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
800 if (old_y
!= m_yScrollPosition
) {
801 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
802 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
808 m_targetWindow
->MacUpdateImmediately();
813 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
815 m_xScrollingEnabled
= x_scroll
;
816 m_yScrollingEnabled
= y_scroll
;
819 void wxScrollHelper::GetVirtualSize (int *x
, int *y
) const
822 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
824 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
827 // Where the current view starts from
828 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
831 *x
= m_xScrollPosition
;
833 *y
= m_yScrollPosition
;
836 void wxScrollHelper::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
839 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
841 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
844 void wxScrollHelper::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
847 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
849 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
852 // ----------------------------------------------------------------------------
854 // ----------------------------------------------------------------------------
856 // Default OnSize resets scrollbars, if any
857 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
859 #if wxUSE_CONSTRAINTS
860 if ( m_win
->GetAutoLayout() )
867 // This calls OnDraw, having adjusted the origin according to the current
869 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
871 // don't use m_targetWindow here, this is always called for ourselves
878 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
879 // compatibility here - if we used OnKeyDown(), the programs which process
880 // arrows themselves in their OnChar() would never get the message and like
881 // this they always have the priority
882 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
884 int stx
, sty
, // view origin
885 szx
, szy
, // view size (total)
886 clix
, cliy
; // view size (on screen)
888 GetViewStart(&stx
, &sty
);
889 GetTargetSize(&clix
, &cliy
);
890 GetVirtualSize(&szx
, &szy
);
892 if( m_xScrollPixelsPerLine
)
894 clix
/= m_xScrollPixelsPerLine
;
895 szx
/= m_xScrollPixelsPerLine
;
902 if( m_yScrollPixelsPerLine
)
904 cliy
/= m_yScrollPixelsPerLine
;
905 szy
/= m_yScrollPixelsPerLine
;
913 int xScrollOld
= m_xScrollPosition
,
914 yScrollOld
= m_yScrollPosition
;
917 switch ( event
.KeyCode() )
921 dsty
= sty
- (5 * cliy
/ 6);
922 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
927 Scroll(-1, sty
+ (5 * cliy
/ 6));
931 Scroll(0, event
.ControlDown() ? 0 : -1);
935 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
959 if ( m_xScrollPosition
!= xScrollOld
)
961 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
963 event
.SetEventObject(m_win
);
964 m_win
->GetEventHandler()->ProcessEvent(event
);
967 if ( m_yScrollPosition
!= yScrollOld
)
969 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
971 event
.SetEventObject(m_win
);
972 m_win
->GetEventHandler()->ProcessEvent(event
);
976 // ----------------------------------------------------------------------------
977 // autoscroll stuff: these functions deal with sending fake scroll events when
978 // a captured mouse is being held outside the window
979 // ----------------------------------------------------------------------------
981 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
983 // only send the event if the window is scrollable in this direction
984 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
985 return win
->HasScrollbar(event
.GetOrientation());
988 void wxScrollHelper::StopAutoScrolling()
990 if ( m_timerAutoScroll
)
992 delete m_timerAutoScroll
;
993 m_timerAutoScroll
= (wxTimer
*)NULL
;
997 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1004 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1006 // don't prevent the usual processing of the event from taking place
1009 // when a captured mouse leave a scrolled window we start generate
1010 // scrolling events to allow, for example, extending selection beyond the
1011 // visible area in some controls
1012 if ( wxWindow::GetCapture() == m_targetWindow
)
1014 // where is the mouse leaving?
1016 wxPoint pt
= event
.GetPosition();
1019 orient
= wxHORIZONTAL
;
1022 else if ( pt
.y
< 0 )
1024 orient
= wxVERTICAL
;
1027 else // we're lower or to the right of the window
1029 wxSize size
= m_targetWindow
->GetClientSize();
1030 if ( pt
.x
> size
.x
)
1032 orient
= wxHORIZONTAL
;
1033 pos
= m_xScrollLines
;
1035 else if ( pt
.y
> size
.y
)
1037 orient
= wxVERTICAL
;
1038 pos
= m_yScrollLines
;
1040 else // this should be impossible
1042 // but seems to happen sometimes under wxMSW - maybe it's a bug
1043 // there but for now just ignore it
1045 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1051 // only start the auto scroll timer if the window can be scrolled in
1053 if ( !m_targetWindow
->HasScrollbar(orient
) )
1056 delete m_timerAutoScroll
;
1057 m_timerAutoScroll
= new wxAutoScrollTimer
1059 m_targetWindow
, this,
1060 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1061 : wxEVT_SCROLLWIN_LINEDOWN
,
1065 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1069 #if wxUSE_MOUSEWHEEL
1071 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1073 m_wheelRotation
+= event
.GetWheelRotation();
1074 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1075 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1079 lines
*= event
.GetLinesPerAction();
1081 wxScrollWinEvent newEvent
;
1083 newEvent
.SetPosition(0);
1084 newEvent
.SetOrientation(wxVERTICAL
);
1085 newEvent
.m_eventObject
= m_win
;
1087 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEUP
;
1089 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
1091 int times
= abs(lines
);
1092 for (; times
> 0; times
--)
1093 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1097 // GetViewStart(&vsx, &vsy);
1098 // Scroll(-1, vsy - lines);
1102 #endif // wxUSE_MOUSEWHEEL
1104 // ----------------------------------------------------------------------------
1105 // wxGenericScrolledWindow implementation
1106 // ----------------------------------------------------------------------------
1108 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1110 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1111 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1114 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1119 const wxString
& name
)
1121 m_targetWindow
= this;
1123 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1126 // we need to process arrows ourselves for scrolling
1127 m_lDlgCode
|= DLGC_WANTARROWS
;
1133 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1137 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1139 // the user code didn't really draw the window if we got here, so set this
1140 // flag to try to call OnDraw() later
1141 m_handler
->ResetDrawnFlag();
1146 #if WXWIN_COMPATIBILITY
1148 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
1150 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
1151 *y_page
= GetScrollPageSize(wxVERTICAL
);
1154 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
1157 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
1159 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
1162 #endif // WXWIN_COMPATIBILITY