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"
46 #include <windows.h> // for DLGC_WANTARROWS
50 // For wxRETAINED implementation
51 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
52 //This code switches off the compiler warnings
53 # pragma message disable nosimpint
57 # pragma message enable nosimpint
61 IMPLEMENT_CLASS(wxScrolledWindow
, wxGenericScrolledWindow
)
63 // ----------------------------------------------------------------------------
64 // wxScrollHelperEvtHandler: intercept the events from the window and forward
65 // them to wxScrollHelper
66 // ----------------------------------------------------------------------------
68 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
71 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
73 m_scrollHelper
= scrollHelper
;
76 virtual bool ProcessEvent(wxEvent
& event
);
78 void ResetDrawnFlag() { m_hasDrawnWindow
= FALSE
; }
81 wxScrollHelper
*m_scrollHelper
;
83 bool m_hasDrawnWindow
;
86 // ----------------------------------------------------------------------------
87 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
88 // a captured mouse is held outside the window
89 // ----------------------------------------------------------------------------
91 class wxAutoScrollTimer
: public wxTimer
94 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
95 wxEventType eventTypeToSend
,
98 virtual void Notify();
102 wxScrollHelper
*m_scrollHelper
;
103 wxEventType m_eventType
;
108 // ============================================================================
110 // ============================================================================
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
117 wxScrollHelper
*scroll
,
118 wxEventType eventTypeToSend
,
122 m_scrollHelper
= scroll
;
123 m_eventType
= eventTypeToSend
;
128 void wxAutoScrollTimer::Notify()
130 // only do all this as long as the window is capturing the mouse
131 if ( wxWindow::GetCapture() != m_win
)
135 else // we still capture the mouse, continue generating events
137 // first scroll the window if we are allowed to do it
138 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
139 event1
.SetEventObject(m_win
);
140 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
141 m_win
->GetEventHandler()->ProcessEvent(event1
) )
143 // and then send a pseudo mouse-move event to refresh the selection
144 wxMouseEvent
event2(wxEVT_MOTION
);
145 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
147 // the mouse event coordinates should be client, not screen as
148 // returned by wxGetMousePosition
149 wxWindow
*parentTop
= m_win
;
150 while ( parentTop
->GetParent() )
151 parentTop
= parentTop
->GetParent();
152 wxPoint ptOrig
= parentTop
->GetPosition();
153 event2
.m_x
-= ptOrig
.x
;
154 event2
.m_y
-= ptOrig
.y
;
156 event2
.SetEventObject(m_win
);
158 // FIXME: we don't fill in the other members - ok?
160 m_win
->GetEventHandler()->ProcessEvent(event2
);
162 else // can't scroll further, stop
169 // ----------------------------------------------------------------------------
170 // wxScrollHelperEvtHandler
171 // ----------------------------------------------------------------------------
173 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
175 wxEventType evType
= event
.GetEventType();
177 // the explanation of wxEVT_PAINT processing hack: for historic reasons
178 // there are 2 ways to process this event in classes deriving from
179 // wxScrolledWindow. The user code may
181 // 1. override wxScrolledWindow::OnDraw(dc)
182 // 2. define its own OnPaint() handler
184 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
185 // always processes the draw event, so we can't just try the window
186 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
187 // (the latter would never be called in wxUniversal).
189 // So the solution is to have a flag telling us whether the user code drew
190 // anything in the window. We set it to true here but reset it to false in
191 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
192 // user code defined OnPaint() in the derived class)
193 m_hasDrawnWindow
= TRUE
;
195 // pass it on to the real handler
196 bool processed
= wxEvtHandler::ProcessEvent(event
);
198 // always process the size events ourselves, even if the user code handles
199 // them as well, as we need to AdjustScrollbars()
201 // NB: it is important to do it after processing the event in the normal
202 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
203 // scrollbar[s] (dis)appear and it should be seen by the user code
205 if ( evType
== wxEVT_SIZE
)
207 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
214 // normally, nothing more to do here - except if it was a paint event
215 // which wasn't really processed, then we'll try to call our
216 // OnDraw() below (from HandleOnPaint)
217 if ( m_hasDrawnWindow
)
223 // reset the skipped flag to FALSE as it might have been set to TRUE in
224 // ProcessEvent() above
227 if ( evType
== wxEVT_PAINT
)
229 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
233 if ( evType
== wxEVT_SCROLLWIN_TOP
||
234 evType
== wxEVT_SCROLLWIN_BOTTOM
||
235 evType
== wxEVT_SCROLLWIN_LINEUP
||
236 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
237 evType
== wxEVT_SCROLLWIN_PAGEUP
||
238 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
239 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
240 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
242 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
243 return !event
.GetSkipped();
246 if ( evType
== wxEVT_ENTER_WINDOW
)
248 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
250 else if ( evType
== wxEVT_LEAVE_WINDOW
)
252 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
255 else if ( evType
== wxEVT_MOUSEWHEEL
)
257 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
259 #endif // wxUSE_MOUSEWHEEL
260 else if ( evType
== wxEVT_CHAR
)
262 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
263 return !event
.GetSkipped();
269 // ----------------------------------------------------------------------------
270 // wxScrollHelper construction
271 // ----------------------------------------------------------------------------
273 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
275 m_xScrollPixelsPerLine
=
276 m_yScrollPixelsPerLine
=
281 m_xScrollLinesPerPage
=
282 m_yScrollLinesPerPage
= 0;
284 m_xScrollingEnabled
=
285 m_yScrollingEnabled
= TRUE
;
294 m_targetWindow
= (wxWindow
*)NULL
;
296 m_timerAutoScroll
= (wxTimer
*)NULL
;
304 wxScrollHelper::~wxScrollHelper()
311 // ----------------------------------------------------------------------------
312 // setting scrolling parameters
313 // ----------------------------------------------------------------------------
315 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
325 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
328 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
329 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
*noUnitsX
) ||
331 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
332 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
*noUnitsY
) ||
333 (xPos
!= m_xScrollPosition
) ||
334 (yPos
!= m_yScrollPosition
)
337 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
338 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
339 m_xScrollPosition
= xPos
;
340 m_yScrollPosition
= yPos
;
341 m_xScrollLines
= noUnitsX
;
342 m_yScrollLines
= noUnitsY
;
345 // Sorry, some Motif-specific code to implement a backing pixmap
346 // for the wxRETAINED style. Implementing a backing store can't
347 // be entirely generic because it relies on the wxWindowDC implementation
348 // to duplicate X drawing calls for the backing pixmap.
350 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
352 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
354 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
355 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
356 if (m_targetWindow
->GetBackingPixmap() &&
357 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
358 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
360 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
361 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
364 if (!m_targetWindow
->GetBackingPixmap() &&
365 (noUnitsX
!= 0) && (noUnitsY
!= 0))
367 int depth
= wxDisplayDepth();
368 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
369 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
370 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
371 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
379 if (do_refresh
&& !noRefresh
)
380 m_targetWindow
->Refresh(TRUE
, GetRect());
383 m_targetWindow
->MacUpdateImmediately() ;
387 // ----------------------------------------------------------------------------
388 // [target] window handling
389 // ----------------------------------------------------------------------------
391 void wxScrollHelper::DeleteEvtHandler()
393 // search for m_handler in the handler list
394 if ( m_win
&& m_handler
)
396 if ( m_win
->RemoveEventHandler(m_handler
) )
400 //else: something is very wrong, so better [maybe] leak memory than
401 // risk a crash because of double deletion
407 void wxScrollHelper::SetWindow(wxWindow
*win
)
409 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
413 // by default, the associated window is also the target window
414 DoSetTargetWindow(win
);
417 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
419 m_targetWindow
= target
;
421 // install the event handler which will intercept the events we're
422 // interested in (but only do it for our real window, not the target window
423 // which we scroll - we don't need to hijack its events)
424 if ( m_targetWindow
== m_win
)
426 // if we already have a handler, delete it first
429 m_handler
= new wxScrollHelperEvtHandler(this);
430 m_targetWindow
->PushEventHandler(m_handler
);
434 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
436 wxCHECK_RET( target
, wxT("target window must not be NULL") );
438 if ( target
== m_targetWindow
)
441 DoSetTargetWindow(target
);
444 wxWindow
*wxScrollHelper::GetTargetWindow() const
446 return m_targetWindow
;
449 // ----------------------------------------------------------------------------
450 // scrolling implementation itself
451 // ----------------------------------------------------------------------------
453 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
455 int nScrollInc
= CalcScrollInc(event
);
456 if ( nScrollInc
== 0 )
458 // can't scroll further
464 int orient
= event
.GetOrientation();
465 if (orient
== wxHORIZONTAL
)
467 m_xScrollPosition
+= nScrollInc
;
468 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
472 m_yScrollPosition
+= nScrollInc
;
473 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
476 bool needsRefresh
= FALSE
;
479 if (orient
== wxHORIZONTAL
)
481 if ( m_xScrollingEnabled
)
483 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
492 if ( m_yScrollingEnabled
)
494 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
504 m_targetWindow
->Refresh(TRUE
, GetRect());
508 m_targetWindow
->ScrollWindow(dx
, dy
, GetRect());
512 m_targetWindow
->MacUpdateImmediately() ;
516 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
518 int pos
= event
.GetPosition();
519 int orient
= event
.GetOrientation();
522 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
524 if (orient
== wxHORIZONTAL
)
525 nScrollInc
= - m_xScrollPosition
;
527 nScrollInc
= - m_yScrollPosition
;
529 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
531 if (orient
== wxHORIZONTAL
)
532 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
534 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
536 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
540 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
544 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
546 if (orient
== wxHORIZONTAL
)
547 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
549 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
551 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
553 if (orient
== wxHORIZONTAL
)
554 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
556 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
558 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
559 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
561 if (orient
== wxHORIZONTAL
)
562 nScrollInc
= pos
- m_xScrollPosition
;
564 nScrollInc
= pos
- m_yScrollPosition
;
567 if (orient
== wxHORIZONTAL
)
569 if (m_xScrollPixelsPerLine
> 0)
572 GetTargetSize(&w
, &h
);
574 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
575 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
579 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
580 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
581 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
582 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
585 m_targetWindow
->Refresh(TRUE
, GetRect());
589 if (m_yScrollPixelsPerLine
> 0)
592 GetTargetSize(&w
, &h
);
594 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
595 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
599 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
600 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
601 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
602 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
605 m_targetWindow
->Refresh(TRUE
, GetRect());
611 // Adjust the scrollbars - new version.
612 void wxScrollHelper::AdjustScrollbars()
615 m_targetWindow
->MacUpdateImmediately();
619 GetTargetSize(&w
, &h
);
621 int oldXScroll
= m_xScrollPosition
;
622 int oldYScroll
= m_yScrollPosition
;
624 if (m_xScrollLines
> 0)
626 // Calculate page size i.e. number of scroll units you get on the
627 // current client window
628 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
629 if (noPagePositions
< 1) noPagePositions
= 1;
630 if ( noPagePositions
> m_xScrollLines
)
631 noPagePositions
= m_xScrollLines
;
633 // Correct position if greater than extent of canvas minus
634 // the visible portion of it or if below zero
635 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
636 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
638 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
639 // The amount by which we scroll when paging
640 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
644 m_xScrollPosition
= 0;
645 m_win
->SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
648 if (m_yScrollLines
> 0)
650 // Calculate page size i.e. number of scroll units you get on the
651 // current client window
652 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
653 if (noPagePositions
< 1) noPagePositions
= 1;
654 if ( noPagePositions
> m_yScrollLines
)
655 noPagePositions
= m_yScrollLines
;
657 // Correct position if greater than extent of canvas minus
658 // the visible portion of it or if below zero
659 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
660 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
662 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, m_yScrollLines
);
663 // The amount by which we scroll when paging
664 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
668 m_yScrollPosition
= 0;
669 m_win
->SetScrollbar (wxVERTICAL
, 0, 0, 0, FALSE
);
672 if (oldXScroll
!= m_xScrollPosition
)
674 if (m_xScrollingEnabled
)
675 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
-m_xScrollPosition
), 0,
678 m_targetWindow
->Refresh(TRUE
, GetRect());
681 if (oldYScroll
!= m_yScrollPosition
)
683 if (m_yScrollingEnabled
)
684 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
687 m_targetWindow
->Refresh(TRUE
, GetRect());
691 m_targetWindow
->MacUpdateImmediately();
695 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
697 wxPoint pt
= dc
.GetDeviceOrigin();
698 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
699 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
700 dc
.SetUserScale( m_scaleX
, m_scaleY
);
703 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
706 *x_unit
= m_xScrollPixelsPerLine
;
708 *y_unit
= m_yScrollPixelsPerLine
;
711 int wxScrollHelper::GetScrollPageSize(int orient
) const
713 if ( orient
== wxHORIZONTAL
)
714 return m_xScrollLinesPerPage
;
716 return m_yScrollLinesPerPage
;
719 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
721 if ( orient
== wxHORIZONTAL
)
722 m_xScrollLinesPerPage
= pageSize
;
724 m_yScrollLinesPerPage
= pageSize
;
728 * Scroll to given position (scroll position, not pixel position)
730 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
735 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
736 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
739 m_targetWindow
->MacUpdateImmediately();
743 GetTargetSize(&w
, &h
);
745 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
747 int old_x
= m_xScrollPosition
;
748 m_xScrollPosition
= x_pos
;
750 // Calculate page size i.e. number of scroll units you get on the
751 // current client window
752 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
753 if (noPagePositions
< 1) noPagePositions
= 1;
755 // Correct position if greater than extent of canvas minus
756 // the visible portion of it or if below zero
757 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
758 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
760 if (old_x
!= m_xScrollPosition
) {
761 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
762 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
766 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
768 int old_y
= m_yScrollPosition
;
769 m_yScrollPosition
= y_pos
;
771 // Calculate page size i.e. number of scroll units you get on the
772 // current client window
773 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
774 if (noPagePositions
< 1) noPagePositions
= 1;
776 // Correct position if greater than extent of canvas minus
777 // the visible portion of it or if below zero
778 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
779 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
781 if (old_y
!= m_yScrollPosition
) {
782 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
783 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
789 m_targetWindow
->MacUpdateImmediately();
794 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
796 m_xScrollingEnabled
= x_scroll
;
797 m_yScrollingEnabled
= y_scroll
;
800 void wxScrollHelper::GetVirtualSize (int *x
, int *y
) const
803 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
805 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
808 // Where the current view starts from
809 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
812 *x
= m_xScrollPosition
;
814 *y
= m_yScrollPosition
;
817 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
820 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
822 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
825 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
828 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
830 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
833 // ----------------------------------------------------------------------------
835 // ----------------------------------------------------------------------------
837 // Default OnSize resets scrollbars, if any
838 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
843 // This calls OnDraw, having adjusted the origin according to the current
845 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
847 // don't use m_targetWindow here, this is always called for ourselves
854 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
855 // compatibility here - if we used OnKeyDown(), the programs which process
856 // arrows themselves in their OnChar() would never get the message and like
857 // this they always have the priority
858 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
860 int stx
, sty
, // view origin
861 szx
, szy
, // view size (total)
862 clix
, cliy
; // view size (on screen)
864 GetViewStart(&stx
, &sty
);
865 GetTargetSize(&clix
, &cliy
);
866 GetVirtualSize(&szx
, &szy
);
868 if( m_xScrollPixelsPerLine
)
870 clix
/= m_xScrollPixelsPerLine
;
871 szx
/= m_xScrollPixelsPerLine
;
878 if( m_yScrollPixelsPerLine
)
880 cliy
/= m_yScrollPixelsPerLine
;
881 szy
/= m_yScrollPixelsPerLine
;
889 int xScrollOld
= m_xScrollPosition
,
890 yScrollOld
= m_yScrollPosition
;
893 switch ( event
.KeyCode() )
897 dsty
= sty
- (5 * cliy
/ 6);
898 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
903 Scroll(-1, sty
+ (5 * cliy
/ 6));
907 Scroll(0, event
.ControlDown() ? 0 : -1);
911 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
935 if ( m_xScrollPosition
!= xScrollOld
)
937 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
939 event
.SetEventObject(m_win
);
940 m_win
->GetEventHandler()->ProcessEvent(event
);
943 if ( m_yScrollPosition
!= yScrollOld
)
945 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
947 event
.SetEventObject(m_win
);
948 m_win
->GetEventHandler()->ProcessEvent(event
);
952 // ----------------------------------------------------------------------------
953 // autoscroll stuff: these functions deal with sending fake scroll events when
954 // a captured mouse is being held outside the window
955 // ----------------------------------------------------------------------------
957 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
959 // only send the event if the window is scrollable in this direction
960 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
961 return win
->HasScrollbar(event
.GetOrientation());
964 void wxScrollHelper::StopAutoScrolling()
966 if ( m_timerAutoScroll
)
968 delete m_timerAutoScroll
;
969 m_timerAutoScroll
= (wxTimer
*)NULL
;
973 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
980 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
982 // don't prevent the usual processing of the event from taking place
985 // when a captured mouse leave a scrolled window we start generate
986 // scrolling events to allow, for example, extending selection beyond the
987 // visible area in some controls
988 if ( wxWindow::GetCapture() == m_targetWindow
)
990 // where is the mouse leaving?
992 wxPoint pt
= event
.GetPosition();
995 orient
= wxHORIZONTAL
;
1000 orient
= wxVERTICAL
;
1003 else // we're lower or to the right of the window
1005 wxSize size
= m_targetWindow
->GetClientSize();
1006 if ( pt
.x
> size
.x
)
1008 orient
= wxHORIZONTAL
;
1009 pos
= m_xScrollLines
;
1011 else if ( pt
.y
> size
.y
)
1013 orient
= wxVERTICAL
;
1014 pos
= m_yScrollLines
;
1016 else // this should be impossible
1018 // but seems to happen sometimes under wxMSW - maybe it's a bug
1019 // there but for now just ignore it
1021 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1027 // only start the auto scroll timer if the window can be scrolled in
1029 if ( !m_targetWindow
->HasScrollbar(orient
) )
1032 delete m_timerAutoScroll
;
1033 m_timerAutoScroll
= new wxAutoScrollTimer
1035 m_targetWindow
, this,
1036 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1037 : wxEVT_SCROLLWIN_LINEDOWN
,
1041 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1045 #if wxUSE_MOUSEWHEEL
1047 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1049 m_wheelRotation
+= event
.GetWheelRotation();
1050 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1051 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1055 lines
*= event
.GetLinesPerAction();
1057 wxScrollWinEvent newEvent
;
1059 newEvent
.SetPosition(0);
1060 newEvent
.SetOrientation(wxVERTICAL
);
1061 newEvent
.m_eventObject
= m_win
;
1063 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEUP
;
1065 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
1067 int times
= abs(lines
);
1068 for (; times
> 0; times
--)
1069 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1073 // GetViewStart(&vsx, &vsy);
1074 // Scroll(-1, vsy - lines);
1078 #endif // wxUSE_MOUSEWHEEL
1080 // ----------------------------------------------------------------------------
1081 // wxGenericScrolledWindow implementation
1082 // ----------------------------------------------------------------------------
1084 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1086 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1087 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1090 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1095 const wxString
& name
)
1097 m_targetWindow
= this;
1099 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1104 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1108 bool wxGenericScrolledWindow::Layout()
1112 // Take into account the virtual size and scrolled position of the window
1114 CalcScrolledPosition(0,0, &x
,&y
);
1115 GetVirtualSize(&w
, &h
);
1116 GetSizer()->SetDimension(x
, y
, w
, h
);
1120 // fall back to default for LayoutConstraints
1121 return wxPanel::Layout();
1124 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1126 // the user code didn't really draw the window if we got here, so set this
1127 // flag to try to call OnDraw() later
1128 m_handler
->ResetDrawnFlag();
1135 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1139 long rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1141 // we need to process arrows ourselves for scrolling
1142 if ( nMsg
== WM_GETDLGCODE
)
1144 rc
|= DLGC_WANTARROWS
;
1152 #if WXWIN_COMPATIBILITY
1154 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
1156 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
1157 *y_page
= GetScrollPageSize(wxVERTICAL
);
1160 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
1163 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
1165 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
1168 #endif // WXWIN_COMPATIBILITY