1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/scrolwin.cpp
3 // Purpose: wxGenericScrolledWindow implementation
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin on 31.08.00: wxScrollHelper allows to implement.
6 // Ron Lee on 10.4.02: virtual size / auto scrollbars et al.
9 // Copyright: (c) wxWindows team
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
22 #pragma implementation "genscrolwin.h"
26 #define XtDisplay XTDISPLAY
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
36 #if !defined(__WXGTK__) || defined(__WXUNIVERSAL__)
39 #include "wx/dcclient.h"
41 #include "wx/scrolwin.h"
47 #include <windows.h> // for DLGC_WANTARROWS
51 // For wxRETAINED implementation
52 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
53 //This code switches off the compiler warnings
54 # pragma message disable nosimpint
58 # pragma message enable nosimpint
62 IMPLEMENT_CLASS(wxScrolledWindow
, wxGenericScrolledWindow
)
64 // ----------------------------------------------------------------------------
65 // wxScrollHelperEvtHandler: intercept the events from the window and forward
66 // them to wxScrollHelper
67 // ----------------------------------------------------------------------------
69 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
72 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
74 m_scrollHelper
= scrollHelper
;
77 virtual bool ProcessEvent(wxEvent
& event
);
79 void ResetDrawnFlag() { m_hasDrawnWindow
= FALSE
; }
82 wxScrollHelper
*m_scrollHelper
;
84 bool m_hasDrawnWindow
;
86 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
89 // ----------------------------------------------------------------------------
90 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
91 // a captured mouse is held outside the window
92 // ----------------------------------------------------------------------------
94 class wxAutoScrollTimer
: public wxTimer
97 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
98 wxEventType eventTypeToSend
,
101 virtual void Notify();
105 wxScrollHelper
*m_scrollHelper
;
106 wxEventType m_eventType
;
110 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
113 // ============================================================================
115 // ============================================================================
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
122 wxScrollHelper
*scroll
,
123 wxEventType eventTypeToSend
,
127 m_scrollHelper
= scroll
;
128 m_eventType
= eventTypeToSend
;
133 void wxAutoScrollTimer::Notify()
135 // only do all this as long as the window is capturing the mouse
136 if ( wxWindow::GetCapture() != m_win
)
140 else // we still capture the mouse, continue generating events
142 // first scroll the window if we are allowed to do it
143 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
144 event1
.SetEventObject(m_win
);
145 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
146 m_win
->GetEventHandler()->ProcessEvent(event1
) )
148 // and then send a pseudo mouse-move event to refresh the selection
149 wxMouseEvent
event2(wxEVT_MOTION
);
150 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
152 // the mouse event coordinates should be client, not screen as
153 // returned by wxGetMousePosition
154 wxWindow
*parentTop
= m_win
;
155 while ( parentTop
->GetParent() )
156 parentTop
= parentTop
->GetParent();
157 wxPoint ptOrig
= parentTop
->GetPosition();
158 event2
.m_x
-= ptOrig
.x
;
159 event2
.m_y
-= ptOrig
.y
;
161 event2
.SetEventObject(m_win
);
163 // FIXME: we don't fill in the other members - ok?
165 m_win
->GetEventHandler()->ProcessEvent(event2
);
167 else // can't scroll further, stop
174 // ----------------------------------------------------------------------------
175 // wxScrollHelperEvtHandler
176 // ----------------------------------------------------------------------------
178 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
180 wxEventType evType
= event
.GetEventType();
182 // the explanation of wxEVT_PAINT processing hack: for historic reasons
183 // there are 2 ways to process this event in classes deriving from
184 // wxScrolledWindow. The user code may
186 // 1. override wxScrolledWindow::OnDraw(dc)
187 // 2. define its own OnPaint() handler
189 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
190 // always processes the draw event, so we can't just try the window
191 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
192 // (the latter would never be called in wxUniversal).
194 // So the solution is to have a flag telling us whether the user code drew
195 // anything in the window. We set it to true here but reset it to false in
196 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
197 // user code defined OnPaint() in the derived class)
198 m_hasDrawnWindow
= TRUE
;
200 // pass it on to the real handler
201 bool processed
= wxEvtHandler::ProcessEvent(event
);
203 // always process the size events ourselves, even if the user code handles
204 // them as well, as we need to AdjustScrollbars()
206 // NB: it is important to do it after processing the event in the normal
207 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
208 // scrollbar[s] (dis)appear and it should be seen by the user code
210 if ( evType
== wxEVT_SIZE
)
212 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
219 // normally, nothing more to do here - except if it was a paint event
220 // which wasn't really processed, then we'll try to call our
221 // OnDraw() below (from HandleOnPaint)
222 if ( m_hasDrawnWindow
)
228 // reset the skipped flag to FALSE as it might have been set to TRUE in
229 // ProcessEvent() above
232 if ( evType
== wxEVT_PAINT
)
234 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
238 if ( evType
== wxEVT_SCROLLWIN_TOP
||
239 evType
== wxEVT_SCROLLWIN_BOTTOM
||
240 evType
== wxEVT_SCROLLWIN_LINEUP
||
241 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
242 evType
== wxEVT_SCROLLWIN_PAGEUP
||
243 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
244 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
245 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
247 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
248 return !event
.GetSkipped();
251 if ( evType
== wxEVT_ENTER_WINDOW
)
253 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
255 else if ( evType
== wxEVT_LEAVE_WINDOW
)
257 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
260 else if ( evType
== wxEVT_MOUSEWHEEL
)
262 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
264 #endif // wxUSE_MOUSEWHEEL
265 else if ( evType
== wxEVT_CHAR
)
267 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
268 return !event
.GetSkipped();
274 // ----------------------------------------------------------------------------
275 // wxScrollHelper construction
276 // ----------------------------------------------------------------------------
278 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
280 m_xScrollPixelsPerLine
=
281 m_yScrollPixelsPerLine
=
286 m_xScrollLinesPerPage
=
287 m_yScrollLinesPerPage
= 0;
289 m_xScrollingEnabled
=
290 m_yScrollingEnabled
= TRUE
;
299 m_targetWindow
= (wxWindow
*)NULL
;
301 m_timerAutoScroll
= (wxTimer
*)NULL
;
309 wxScrollHelper::~wxScrollHelper()
316 // ----------------------------------------------------------------------------
317 // setting scrolling parameters
318 // ----------------------------------------------------------------------------
320 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
330 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
333 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
334 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
336 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
337 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
338 (xPos
!= m_xScrollPosition
) ||
339 (yPos
!= m_yScrollPosition
)
342 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
343 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
344 m_xScrollPosition
= xPos
;
345 m_yScrollPosition
= yPos
;
347 int w
= noUnitsX
* pixelsPerUnitX
;
348 int h
= noUnitsY
* pixelsPerUnitY
;
350 // For better backward compatibility we set persisting limits
351 // here not just the size. It makes SetScrollbars 'sticky'
352 // emulating the old non-autoscroll behaviour.
354 m_targetWindow
->SetVirtualSizeHints( w
, h
);
356 // The above should arguably be deprecated, this however we still need.
358 m_targetWindow
->SetVirtualSize( w
, h
);
360 if (do_refresh
&& !noRefresh
)
361 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
363 #ifndef __WXUNIVERSAL__
364 // If the target is not the same as the window with the scrollbars,
365 // then we need to update the scrollbars here, since they won't have
366 // been updated by SetVirtualSize().
367 if ( m_targetWindow
!= m_win
)
368 #endif // !__WXUNIVERSAL__
372 #ifndef __WXUNIVERSAL__
375 // otherwise this has been done by AdjustScrollbars, above
377 m_targetWindow
->Update() ;
380 #endif // !__WXUNIVERSAL__
383 // ----------------------------------------------------------------------------
384 // [target] window handling
385 // ----------------------------------------------------------------------------
387 void wxScrollHelper::DeleteEvtHandler()
389 // search for m_handler in the handler list
390 if ( m_win
&& m_handler
)
392 if ( m_win
->RemoveEventHandler(m_handler
) )
396 //else: something is very wrong, so better [maybe] leak memory than
397 // risk a crash because of double deletion
403 void wxScrollHelper::SetWindow(wxWindow
*win
)
405 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
409 // by default, the associated window is also the target window
410 DoSetTargetWindow(win
);
413 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
415 m_targetWindow
= target
;
417 // install the event handler which will intercept the events we're
418 // interested in (but only do it for our real window, not the target window
419 // which we scroll - we don't need to hijack its events)
420 if ( m_targetWindow
== m_win
)
422 // if we already have a handler, delete it first
425 m_handler
= new wxScrollHelperEvtHandler(this);
426 m_targetWindow
->PushEventHandler(m_handler
);
430 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
432 wxCHECK_RET( target
, wxT("target window must not be NULL") );
434 if ( target
== m_targetWindow
)
437 DoSetTargetWindow(target
);
440 wxWindow
*wxScrollHelper::GetTargetWindow() const
442 return m_targetWindow
;
445 // ----------------------------------------------------------------------------
446 // scrolling implementation itself
447 // ----------------------------------------------------------------------------
449 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
451 int nScrollInc
= CalcScrollInc(event
);
452 if ( nScrollInc
== 0 )
454 // can't scroll further
460 int orient
= event
.GetOrientation();
461 if (orient
== wxHORIZONTAL
)
463 m_xScrollPosition
+= nScrollInc
;
464 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
468 m_yScrollPosition
+= nScrollInc
;
469 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
472 bool needsRefresh
= FALSE
;
475 if (orient
== wxHORIZONTAL
)
477 if ( m_xScrollingEnabled
)
479 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
488 if ( m_yScrollingEnabled
)
490 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
500 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
504 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
508 m_targetWindow
->Update() ;
512 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
514 int pos
= event
.GetPosition();
515 int orient
= event
.GetOrientation();
518 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
520 if (orient
== wxHORIZONTAL
)
521 nScrollInc
= - m_xScrollPosition
;
523 nScrollInc
= - m_yScrollPosition
;
525 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
527 if (orient
== wxHORIZONTAL
)
528 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
530 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
532 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
536 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
540 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
542 if (orient
== wxHORIZONTAL
)
543 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
545 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
547 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
549 if (orient
== wxHORIZONTAL
)
550 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
552 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
554 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
555 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
557 if (orient
== wxHORIZONTAL
)
558 nScrollInc
= pos
- m_xScrollPosition
;
560 nScrollInc
= pos
- m_yScrollPosition
;
563 if (orient
== wxHORIZONTAL
)
565 if (m_xScrollPixelsPerLine
> 0)
568 GetTargetSize(&w
, &h
);
570 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
571 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
575 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
576 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
577 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
578 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
581 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
585 if ( m_yScrollPixelsPerLine
> 0 )
587 if ( m_yScrollPosition
+ nScrollInc
< 0 )
589 // As -ve as we can go
590 nScrollInc
= -m_yScrollPosition
;
592 else // check for the other bound
594 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
595 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
597 // As +ve as we can go
598 nScrollInc
= posMax
- m_yScrollPosition
;
604 // VZ: why do we do this? (FIXME)
605 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
612 // Adjust the scrollbars - new version.
613 void wxScrollHelper::AdjustScrollbars()
616 m_targetWindow
->Update();
622 int oldXScroll
= m_xScrollPosition
;
623 int oldYScroll
= m_yScrollPosition
;
625 // VZ: at least under Windows this loop is useless because when scrollbars
626 // [dis]appear we get a WM_SIZE resulting in another call to
627 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
628 // it here for now but it would be better to ensure that all ports
629 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
630 // necessary, and remove it later
633 GetTargetSize(&w
, 0);
635 if (m_xScrollPixelsPerLine
== 0)
638 m_xScrollPosition
= 0;
639 m_win
->SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
643 m_xScrollLines
= m_targetWindow
->GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine
;
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
);
662 GetTargetSize(0, &h
);
664 // scroll lines per page: if 0, no scrolling is needed
667 if ( m_yScrollPixelsPerLine
== 0 )
669 // scrolling is disabled
671 m_yScrollPosition
= 0;
674 else // might need scrolling
676 int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
677 m_yScrollLines
= hVirt
/ m_yScrollPixelsPerLine
;
679 // Calculate page size i.e. number of scroll units you get on the
680 // current client window
681 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
682 if ( linesPerPage
>= m_yScrollLines
)
684 // we're big enough to not need scrolling
687 m_yScrollPosition
= 0;
689 else // we do need a scrollbar
691 if ( linesPerPage
< 1 )
694 // Correct position if greater than extent of canvas minus
695 // the visible portion of it or if below zero
696 const int posMax
= m_yScrollLines
- linesPerPage
;
697 if ( m_yScrollPosition
> posMax
)
698 m_yScrollPosition
= posMax
;
699 else if ( m_yScrollPosition
< 0 )
700 m_yScrollPosition
= 0;
704 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
705 linesPerPage
, m_yScrollLines
);
707 // The amount by which we scroll when paging
708 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
711 // If a scrollbar (dis)appeared as a result of this, adjust them again.
715 GetTargetSize( &w
, &h
);
716 } while ( w
!= oldw
&& h
!= oldh
);
719 // Sorry, some Motif-specific code to implement a backing pixmap
720 // for the wxRETAINED style. Implementing a backing store can't
721 // be entirely generic because it relies on the wxWindowDC implementation
722 // to duplicate X drawing calls for the backing pixmap.
724 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
726 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
728 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
729 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
730 if (m_targetWindow
->GetBackingPixmap() &&
731 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
732 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
734 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
735 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
738 if (!m_targetWindow
->GetBackingPixmap() &&
739 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
741 int depth
= wxDisplayDepth();
742 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
743 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
744 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
745 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
751 if (oldXScroll
!= m_xScrollPosition
)
753 if (m_xScrollingEnabled
)
754 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
757 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
760 if (oldYScroll
!= m_yScrollPosition
)
762 if (m_yScrollingEnabled
)
763 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
766 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
770 m_targetWindow
->Update();
774 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
776 wxPoint pt
= dc
.GetDeviceOrigin();
777 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
778 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
779 dc
.SetUserScale( m_scaleX
, m_scaleY
);
782 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
784 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
785 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
787 m_xScrollPixelsPerLine
= xstep
;
788 m_yScrollPixelsPerLine
= ystep
;
790 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
791 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
793 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
794 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
795 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
800 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
803 *x_unit
= m_xScrollPixelsPerLine
;
805 *y_unit
= m_yScrollPixelsPerLine
;
808 int wxScrollHelper::GetScrollPageSize(int orient
) const
810 if ( orient
== wxHORIZONTAL
)
811 return m_xScrollLinesPerPage
;
813 return m_yScrollLinesPerPage
;
816 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
818 if ( orient
== wxHORIZONTAL
)
819 m_xScrollLinesPerPage
= pageSize
;
821 m_yScrollLinesPerPage
= pageSize
;
825 * Scroll to given position (scroll position, not pixel position)
827 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
832 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
833 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
836 m_targetWindow
->Update();
840 GetTargetSize(&w
, &h
);
842 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
844 int old_x
= m_xScrollPosition
;
845 m_xScrollPosition
= x_pos
;
847 // Calculate page size i.e. number of scroll units you get on the
848 // current client window
849 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
850 if (noPagePositions
< 1) noPagePositions
= 1;
852 // Correct position if greater than extent of canvas minus
853 // the visible portion of it or if below zero
854 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
855 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
857 if (old_x
!= m_xScrollPosition
) {
858 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
859 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
863 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
865 int old_y
= m_yScrollPosition
;
866 m_yScrollPosition
= y_pos
;
868 // Calculate page size i.e. number of scroll units you get on the
869 // current client window
870 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
871 if (noPagePositions
< 1) noPagePositions
= 1;
873 // Correct position if greater than extent of canvas minus
874 // the visible portion of it or if below zero
875 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
876 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
878 if (old_y
!= m_yScrollPosition
) {
879 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
880 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
886 m_targetWindow
->Update();
891 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
893 m_xScrollingEnabled
= x_scroll
;
894 m_yScrollingEnabled
= y_scroll
;
897 // Where the current view starts from
898 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
901 *x
= m_xScrollPosition
;
903 *y
= m_yScrollPosition
;
906 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
909 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
911 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
914 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
917 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
919 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
922 // ----------------------------------------------------------------------------
924 // ----------------------------------------------------------------------------
926 // Default OnSize resets scrollbars, if any
927 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
929 if( m_win
->GetAutoLayout() || m_targetWindow
->GetAutoLayout() )
931 if ( m_targetWindow
!= m_win
)
932 m_targetWindow
->FitInside();
936 // FIXME: Something is really weird here... This should be
937 // called by FitInside above (and apparently is), yet the
938 // scrollsub sample will get the scrollbar wrong if resized
939 // quickly. This masks the bug, but is surely not the right
947 // This calls OnDraw, having adjusted the origin according to the current
949 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
951 // don't use m_targetWindow here, this is always called for ourselves
958 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
959 // compatibility here - if we used OnKeyDown(), the programs which process
960 // arrows themselves in their OnChar() would never get the message and like
961 // this they always have the priority
962 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
964 int stx
, sty
, // view origin
965 szx
, szy
, // view size (total)
966 clix
, cliy
; // view size (on screen)
968 GetViewStart(&stx
, &sty
);
969 GetTargetSize(&clix
, &cliy
);
970 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
972 if( m_xScrollPixelsPerLine
)
974 clix
/= m_xScrollPixelsPerLine
;
975 szx
/= m_xScrollPixelsPerLine
;
982 if( m_yScrollPixelsPerLine
)
984 cliy
/= m_yScrollPixelsPerLine
;
985 szy
/= m_yScrollPixelsPerLine
;
993 int xScrollOld
= m_xScrollPosition
,
994 yScrollOld
= m_yScrollPosition
;
997 switch ( event
.GetKeyCode() )
1001 dsty
= sty
- (5 * cliy
/ 6);
1002 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1007 Scroll(-1, sty
+ (5 * cliy
/ 6));
1011 Scroll(0, event
.ControlDown() ? 0 : -1);
1015 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1019 Scroll(-1, sty
- 1);
1023 Scroll(-1, sty
+ 1);
1027 Scroll(stx
- 1, -1);
1031 Scroll(stx
+ 1, -1);
1039 if ( m_xScrollPosition
!= xScrollOld
)
1041 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1043 event
.SetEventObject(m_win
);
1044 m_win
->GetEventHandler()->ProcessEvent(event
);
1047 if ( m_yScrollPosition
!= yScrollOld
)
1049 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1051 event
.SetEventObject(m_win
);
1052 m_win
->GetEventHandler()->ProcessEvent(event
);
1056 // ----------------------------------------------------------------------------
1057 // autoscroll stuff: these functions deal with sending fake scroll events when
1058 // a captured mouse is being held outside the window
1059 // ----------------------------------------------------------------------------
1061 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1063 // only send the event if the window is scrollable in this direction
1064 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1065 return win
->HasScrollbar(event
.GetOrientation());
1068 void wxScrollHelper::StopAutoScrolling()
1070 if ( m_timerAutoScroll
)
1072 delete m_timerAutoScroll
;
1073 m_timerAutoScroll
= (wxTimer
*)NULL
;
1077 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1079 StopAutoScrolling();
1084 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1086 // don't prevent the usual processing of the event from taking place
1089 // when a captured mouse leave a scrolled window we start generate
1090 // scrolling events to allow, for example, extending selection beyond the
1091 // visible area in some controls
1092 if ( wxWindow::GetCapture() == m_targetWindow
)
1094 // where is the mouse leaving?
1096 wxPoint pt
= event
.GetPosition();
1099 orient
= wxHORIZONTAL
;
1102 else if ( pt
.y
< 0 )
1104 orient
= wxVERTICAL
;
1107 else // we're lower or to the right of the window
1109 wxSize size
= m_targetWindow
->GetClientSize();
1110 if ( pt
.x
> size
.x
)
1112 orient
= wxHORIZONTAL
;
1113 pos
= m_xScrollLines
;
1115 else if ( pt
.y
> size
.y
)
1117 orient
= wxVERTICAL
;
1118 pos
= m_yScrollLines
;
1120 else // this should be impossible
1122 // but seems to happen sometimes under wxMSW - maybe it's a bug
1123 // there but for now just ignore it
1125 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1131 // only start the auto scroll timer if the window can be scrolled in
1133 if ( !m_targetWindow
->HasScrollbar(orient
) )
1136 delete m_timerAutoScroll
;
1137 m_timerAutoScroll
= new wxAutoScrollTimer
1139 m_targetWindow
, this,
1140 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1141 : wxEVT_SCROLLWIN_LINEDOWN
,
1145 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1149 #if wxUSE_MOUSEWHEEL
1151 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1153 m_wheelRotation
+= event
.GetWheelRotation();
1154 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1155 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1160 wxScrollWinEvent newEvent
;
1162 newEvent
.SetPosition(0);
1163 newEvent
.SetOrientation(wxVERTICAL
);
1164 newEvent
.m_eventObject
= m_win
;
1166 if (event
.IsPageScroll())
1169 newEvent
.m_eventType
= wxEVT_SCROLLWIN_PAGEUP
;
1171 newEvent
.m_eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
1173 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1177 lines
*= event
.GetLinesPerAction();
1179 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEUP
;
1181 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
1183 int times
= abs(lines
);
1184 for (; times
> 0; times
--)
1185 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1190 #endif // wxUSE_MOUSEWHEEL
1192 // ----------------------------------------------------------------------------
1193 // wxGenericScrolledWindow implementation
1194 // ----------------------------------------------------------------------------
1196 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1198 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1199 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1202 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1207 const wxString
& name
)
1209 m_targetWindow
= this;
1211 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1216 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1220 bool wxGenericScrolledWindow::Layout()
1222 if (GetSizer() && m_targetWindow
== this)
1224 // If we're the scroll target, take into account the
1225 // virtual size and scrolled position of the window.
1228 CalcScrolledPosition(0,0, &x
,&y
);
1229 GetVirtualSize(&w
, &h
);
1230 GetSizer()->SetDimension(x
, y
, w
, h
);
1234 // fall back to default for LayoutConstraints
1235 return wxPanel::Layout();
1238 void wxGenericScrolledWindow::DoSetVirtualSize(int x
, int y
)
1240 wxPanel::DoSetVirtualSize( x
, y
);
1243 #if wxUSE_CONSTRAINTS
1244 if (GetAutoLayout())
1249 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1251 // the user code didn't really draw the window if we got here, so set this
1252 // flag to try to call OnDraw() later
1253 m_handler
->ResetDrawnFlag();
1260 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1264 long rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1267 // we need to process arrows ourselves for scrolling
1268 if ( nMsg
== WM_GETDLGCODE
)
1270 rc
|= DLGC_WANTARROWS
;
1279 #if WXWIN_COMPATIBILITY
1281 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
1283 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
1284 *y_page
= GetScrollPageSize(wxVERTICAL
);
1287 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
1290 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
1292 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
1295 #endif // WXWIN_COMPATIBILITY