1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/scrlwing.cpp
3 // Purpose: wxScrolledWindow 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) wxWidgets team
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
22 #define XtDisplay XTDISPLAY
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
32 #include "wx/scrolwin.h"
37 #include "wx/dcclient.h"
45 #include "wx/recguard.h"
48 #include <windows.h> // for DLGC_WANTARROWS
49 #include "wx/msw/winundef.h"
53 // For wxRETAINED implementation
54 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
55 //This code switches off the compiler warnings
56 # pragma message disable nosimpint
60 # pragma message enable nosimpint
66 style wxHSCROLL | wxVSCROLL
69 // ----------------------------------------------------------------------------
70 // wxScrollHelperEvtHandler: intercept the events from the window and forward
71 // them to wxScrollHelper
72 // ----------------------------------------------------------------------------
74 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
77 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
79 m_scrollHelper
= scrollHelper
;
82 virtual bool ProcessEvent(wxEvent
& event
);
84 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
87 wxScrollHelper
*m_scrollHelper
;
89 bool m_hasDrawnWindow
;
91 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
95 // ----------------------------------------------------------------------------
96 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
97 // a captured mouse is held outside the window
98 // ----------------------------------------------------------------------------
100 class wxAutoScrollTimer
: public wxTimer
103 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
104 wxEventType eventTypeToSend
,
105 int pos
, int orient
);
107 virtual void Notify();
111 wxScrollHelper
*m_scrollHelper
;
112 wxEventType m_eventType
;
116 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
119 // ============================================================================
121 // ============================================================================
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
128 wxScrollHelper
*scroll
,
129 wxEventType eventTypeToSend
,
133 m_scrollHelper
= scroll
;
134 m_eventType
= eventTypeToSend
;
139 void wxAutoScrollTimer::Notify()
141 // only do all this as long as the window is capturing the mouse
142 if ( wxWindow::GetCapture() != m_win
)
146 else // we still capture the mouse, continue generating events
148 // first scroll the window if we are allowed to do it
149 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
150 event1
.SetEventObject(m_win
);
151 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
152 m_win
->GetEventHandler()->ProcessEvent(event1
) )
154 // and then send a pseudo mouse-move event to refresh the selection
155 wxMouseEvent
event2(wxEVT_MOTION
);
156 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
158 // the mouse event coordinates should be client, not screen as
159 // returned by wxGetMousePosition
160 wxWindow
*parentTop
= m_win
;
161 while ( parentTop
->GetParent() )
162 parentTop
= parentTop
->GetParent();
163 wxPoint ptOrig
= parentTop
->GetPosition();
164 event2
.m_x
-= ptOrig
.x
;
165 event2
.m_y
-= ptOrig
.y
;
167 event2
.SetEventObject(m_win
);
169 // FIXME: we don't fill in the other members - ok?
171 m_win
->GetEventHandler()->ProcessEvent(event2
);
173 else // can't scroll further, stop
181 // ----------------------------------------------------------------------------
182 // wxScrollHelperEvtHandler
183 // ----------------------------------------------------------------------------
185 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
187 wxEventType evType
= event
.GetEventType();
189 // the explanation of wxEVT_PAINT processing hack: for historic reasons
190 // there are 2 ways to process this event in classes deriving from
191 // wxScrolledWindow. The user code may
193 // 1. override wxScrolledWindow::OnDraw(dc)
194 // 2. define its own OnPaint() handler
196 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
197 // always processes the draw event, so we can't just try the window
198 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
199 // (the latter would never be called in wxUniversal).
201 // So the solution is to have a flag telling us whether the user code drew
202 // anything in the window. We set it to true here but reset it to false in
203 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
204 // user code defined OnPaint() in the derived class)
205 m_hasDrawnWindow
= true;
207 // pass it on to the real handler
208 bool processed
= wxEvtHandler::ProcessEvent(event
);
210 // always process the size events ourselves, even if the user code handles
211 // them as well, as we need to AdjustScrollbars()
213 // NB: it is important to do it after processing the event in the normal
214 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
215 // scrollbar[s] (dis)appear and it should be seen by the user code
217 if ( evType
== wxEVT_SIZE
)
219 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
226 // normally, nothing more to do here - except if it was a paint event
227 // which wasn't really processed, then we'll try to call our
228 // OnDraw() below (from HandleOnPaint)
229 if ( m_hasDrawnWindow
|| event
.IsCommandEvent() )
235 // reset the skipped flag to false as it might have been set to true in
236 // ProcessEvent() above
239 if ( evType
== wxEVT_PAINT
)
241 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
245 if ( evType
== wxEVT_SCROLLWIN_TOP
||
246 evType
== wxEVT_SCROLLWIN_BOTTOM
||
247 evType
== wxEVT_SCROLLWIN_LINEUP
||
248 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
249 evType
== wxEVT_SCROLLWIN_PAGEUP
||
250 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
251 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
252 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
254 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
255 return !event
.GetSkipped();
258 if ( evType
== wxEVT_ENTER_WINDOW
)
260 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
262 else if ( evType
== wxEVT_LEAVE_WINDOW
)
264 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
267 else if ( evType
== wxEVT_MOUSEWHEEL
)
269 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
271 #endif // wxUSE_MOUSEWHEEL
272 else if ( evType
== wxEVT_CHAR
)
274 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
275 return !event
.GetSkipped();
281 // ----------------------------------------------------------------------------
282 // wxScrollHelper construction
283 // ----------------------------------------------------------------------------
285 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
287 wxASSERT_MSG( win
, _T("associated window can't be NULL in wxScrollHelper") );
289 m_xScrollPixelsPerLine
=
290 m_yScrollPixelsPerLine
=
295 m_xScrollLinesPerPage
=
296 m_yScrollLinesPerPage
= 0;
298 m_xScrollingEnabled
=
299 m_yScrollingEnabled
= true;
308 m_targetWindow
= (wxWindow
*)NULL
;
310 m_timerAutoScroll
= (wxTimer
*)NULL
;
316 // by default, the associated window is also the target window
317 DoSetTargetWindow(win
);
320 wxScrollHelper::~wxScrollHelper()
327 // ----------------------------------------------------------------------------
328 // setting scrolling parameters
329 // ----------------------------------------------------------------------------
331 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
341 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
344 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
345 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
347 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
348 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
349 (xPos
!= m_xScrollPosition
) ||
350 (yPos
!= m_yScrollPosition
)
353 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
354 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
355 m_xScrollPosition
= xPos
;
356 m_yScrollPosition
= yPos
;
358 int w
= noUnitsX
* pixelsPerUnitX
;
359 int h
= noUnitsY
* pixelsPerUnitY
;
361 // For better backward compatibility we set persisting limits
362 // here not just the size. It makes SetScrollbars 'sticky'
363 // emulating the old non-autoscroll behaviour.
364 // m_targetWindow->SetVirtualSizeHints( w, h );
366 // The above should arguably be deprecated, this however we still need.
368 // take care not to set 0 virtual size, 0 means that we don't have any
369 // scrollbars and hence we should use the real size instead of the virtual
370 // one which is indicated by using wxDefaultCoord
371 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
372 h
? h
: wxDefaultCoord
);
374 if (do_refresh
&& !noRefresh
)
375 m_targetWindow
->Refresh(true, GetScrollRect());
377 #ifndef __WXUNIVERSAL__
378 // If the target is not the same as the window with the scrollbars,
379 // then we need to update the scrollbars here, since they won't have
380 // been updated by SetVirtualSize().
381 if ( m_targetWindow
!= m_win
)
382 #endif // !__WXUNIVERSAL__
386 #ifndef __WXUNIVERSAL__
389 // otherwise this has been done by AdjustScrollbars, above
391 #endif // !__WXUNIVERSAL__
394 // ----------------------------------------------------------------------------
395 // [target] window handling
396 // ----------------------------------------------------------------------------
398 void wxScrollHelper::DeleteEvtHandler()
400 // search for m_handler in the handler list
401 if ( m_win
&& m_handler
)
403 if ( m_win
->RemoveEventHandler(m_handler
) )
407 //else: something is very wrong, so better [maybe] leak memory than
408 // risk a crash because of double deletion
414 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
416 m_targetWindow
= target
;
418 target
->MacSetClipChildren( true ) ;
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, GetScrollRect());
508 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
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)
567 if ( m_xScrollPosition
+ nScrollInc
< 0 )
569 // As -ve as we can go
570 nScrollInc
= -m_xScrollPosition
;
572 else // check for the other bound
574 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
575 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
577 // As +ve as we can go
578 nScrollInc
= posMax
- m_xScrollPosition
;
583 m_targetWindow
->Refresh(true, GetScrollRect());
587 if ( m_yScrollPixelsPerLine
> 0 )
589 if ( m_yScrollPosition
+ nScrollInc
< 0 )
591 // As -ve as we can go
592 nScrollInc
= -m_yScrollPosition
;
594 else // check for the other bound
596 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
597 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
599 // As +ve as we can go
600 nScrollInc
= posMax
- m_yScrollPosition
;
606 // VZ: why do we do this? (FIXME)
607 m_targetWindow
->Refresh(true, GetScrollRect());
614 // Adjust the scrollbars - new version.
615 void wxScrollHelper::AdjustScrollbars()
617 static wxRecursionGuardFlag s_flagReentrancy
;
618 wxRecursionGuard
guard(s_flagReentrancy
);
619 if ( guard
.IsInside() )
621 // don't reenter AdjustScrollbars() while another call to
622 // AdjustScrollbars() is in progress because this may lead to calling
623 // ScrollWindow() twice and this can really happen under MSW if
624 // SetScrollbar() call below adds or removes the scrollbar which
625 // changes the window size and hence results in another
626 // AdjustScrollbars() call
633 int oldXScroll
= m_xScrollPosition
;
634 int oldYScroll
= m_yScrollPosition
;
636 // VZ: at least under Windows this loop is useless because when scrollbars
637 // [dis]appear we get a WM_SIZE resulting in another call to
638 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
639 // it here for now but it would be better to ensure that all ports
640 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
641 // necessary, and remove it later
642 // JACS: Stop potential infinite loop by limiting number of iterations
643 int iterationCount
= 0;
644 const int iterationMax
= 5;
649 GetTargetSize(&w
, 0);
651 // scroll lines per page: if 0, no scrolling is needed
654 if ( m_xScrollPixelsPerLine
== 0 )
656 // scrolling is disabled
658 m_xScrollPosition
= 0;
661 else // might need scrolling
663 // Round up integer division to catch any "leftover" client space.
664 const int wVirt
= m_targetWindow
->GetVirtualSize().GetWidth();
665 m_xScrollLines
= (wVirt
+ m_xScrollPixelsPerLine
- 1) / m_xScrollPixelsPerLine
;
667 // Calculate page size i.e. number of scroll units you get on the
668 // current client window.
669 linesPerPage
= w
/ m_xScrollPixelsPerLine
;
671 // Special case. When client and virtual size are very close but
672 // the client is big enough, kill scrollbar.
673 if ((linesPerPage
< m_xScrollLines
) && (w
>= wVirt
)) ++linesPerPage
;
675 if (linesPerPage
>= m_xScrollLines
)
677 // we're big enough to not need scrolling
680 m_xScrollPosition
= 0;
682 else // we do need a scrollbar
684 if ( linesPerPage
< 1 )
687 // Correct position if greater than extent of canvas minus
688 // the visible portion of it or if below zero
689 const int posMax
= m_xScrollLines
- linesPerPage
;
690 if ( m_xScrollPosition
> posMax
)
691 m_xScrollPosition
= posMax
;
692 else if ( m_xScrollPosition
< 0 )
693 m_xScrollPosition
= 0;
697 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
,
698 linesPerPage
, m_xScrollLines
);
700 // The amount by which we scroll when paging
701 SetScrollPageSize(wxHORIZONTAL
, linesPerPage
);
703 GetTargetSize(0, &h
);
705 if ( m_yScrollPixelsPerLine
== 0 )
707 // scrolling is disabled
709 m_yScrollPosition
= 0;
712 else // might need scrolling
714 // Round up integer division to catch any "leftover" client space.
715 const int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
716 m_yScrollLines
= ( hVirt
+ m_yScrollPixelsPerLine
- 1 ) / m_yScrollPixelsPerLine
;
718 // Calculate page size i.e. number of scroll units you get on the
719 // current client window.
720 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
722 // Special case. When client and virtual size are very close but
723 // the client is big enough, kill scrollbar.
724 if ((linesPerPage
< m_yScrollLines
) && (h
>= hVirt
)) ++linesPerPage
;
726 if (linesPerPage
>= m_yScrollLines
)
728 // we're big enough to not need scrolling
731 m_yScrollPosition
= 0;
733 else // we do need a scrollbar
735 if ( linesPerPage
< 1 )
738 // Correct position if greater than extent of canvas minus
739 // the visible portion of it or if below zero
740 const int posMax
= m_yScrollLines
- linesPerPage
;
741 if ( m_yScrollPosition
> posMax
)
742 m_yScrollPosition
= posMax
;
743 else if ( m_yScrollPosition
< 0 )
744 m_yScrollPosition
= 0;
748 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
749 linesPerPage
, m_yScrollLines
);
751 // The amount by which we scroll when paging
752 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
755 // If a scrollbar (dis)appeared as a result of this, adjust them again.
759 GetTargetSize( &w
, &h
);
760 } while ( (w
!= oldw
|| h
!= oldh
) && (iterationCount
< iterationMax
) );
763 // Sorry, some Motif-specific code to implement a backing pixmap
764 // for the wxRETAINED style. Implementing a backing store can't
765 // be entirely generic because it relies on the wxWindowDC implementation
766 // to duplicate X drawing calls for the backing pixmap.
768 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
770 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
772 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
773 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
774 if (m_targetWindow
->GetBackingPixmap() &&
775 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
776 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
778 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
779 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
782 if (!m_targetWindow
->GetBackingPixmap() &&
783 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
785 int depth
= wxDisplayDepth();
786 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
787 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
788 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
789 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
795 if (oldXScroll
!= m_xScrollPosition
)
797 if (m_xScrollingEnabled
)
798 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
801 m_targetWindow
->Refresh(true, GetScrollRect());
804 if (oldYScroll
!= m_yScrollPosition
)
806 if (m_yScrollingEnabled
)
807 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
810 m_targetWindow
->Refresh(true, GetScrollRect());
814 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
816 wxPoint pt
= dc
.GetDeviceOrigin();
817 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
818 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
819 dc
.SetUserScale( m_scaleX
, m_scaleY
);
822 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
824 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
825 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
827 m_xScrollPixelsPerLine
= xstep
;
828 m_yScrollPixelsPerLine
= ystep
;
830 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
831 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
833 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
834 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
835 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
840 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
843 *x_unit
= m_xScrollPixelsPerLine
;
845 *y_unit
= m_yScrollPixelsPerLine
;
848 int wxScrollHelper::GetScrollPageSize(int orient
) const
850 if ( orient
== wxHORIZONTAL
)
851 return m_xScrollLinesPerPage
;
853 return m_yScrollLinesPerPage
;
856 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
858 if ( orient
== wxHORIZONTAL
)
859 m_xScrollLinesPerPage
= pageSize
;
861 m_yScrollLinesPerPage
= pageSize
;
865 * Scroll to given position (scroll position, not pixel position)
867 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
872 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
873 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
876 GetTargetSize(&w
, &h
);
878 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
880 int old_x
= m_xScrollPosition
;
881 m_xScrollPosition
= x_pos
;
883 // Calculate page size i.e. number of scroll units you get on the
884 // current client window
885 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
886 if (noPagePositions
< 1) noPagePositions
= 1;
888 // Correct position if greater than extent of canvas minus
889 // the visible portion of it or if below zero
890 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
891 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
893 if (old_x
!= m_xScrollPosition
) {
894 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
895 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
899 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
901 int old_y
= m_yScrollPosition
;
902 m_yScrollPosition
= y_pos
;
904 // Calculate page size i.e. number of scroll units you get on the
905 // current client window
906 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
907 if (noPagePositions
< 1) noPagePositions
= 1;
909 // Correct position if greater than extent of canvas minus
910 // the visible portion of it or if below zero
911 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
912 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
914 if (old_y
!= m_yScrollPosition
) {
915 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
916 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
922 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
924 m_xScrollingEnabled
= x_scroll
;
925 m_yScrollingEnabled
= y_scroll
;
928 // Where the current view starts from
929 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
932 *x
= m_xScrollPosition
;
934 *y
= m_yScrollPosition
;
937 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
940 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
942 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
945 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
948 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
950 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
953 // ----------------------------------------------------------------------------
955 // ----------------------------------------------------------------------------
957 bool wxScrollHelper::ScrollLayout()
959 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
961 // If we're the scroll target, take into account the
962 // virtual size and scrolled position of the window.
964 int x
= 0, y
= 0, w
= 0, h
= 0;
965 CalcScrolledPosition(0,0, &x
,&y
);
966 m_win
->GetVirtualSize(&w
, &h
);
967 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
971 // fall back to default for LayoutConstraints
972 return m_win
->wxWindow::Layout();
975 void wxScrollHelper::ScrollDoSetVirtualSize(int x
, int y
)
977 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
980 if (m_win
->GetAutoLayout())
984 // wxWindow's GetBestVirtualSize returns the actual window size,
985 // whereas we want to return the virtual size
986 wxSize
wxScrollHelper::ScrollGetBestVirtualSize() const
988 wxSize
clientSize(m_win
->GetClientSize());
989 if ( m_win
->GetSizer() )
990 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
995 // return the window best size from the given best virtual size
997 wxScrollHelper::ScrollGetWindowSizeForVirtualSize(const wxSize
& size
) const
999 // Only use the content to set the window size in the direction
1000 // where there's no scrolling; otherwise we're going to get a huge
1001 // window in the direction in which scrolling is enabled
1003 GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1005 wxSize minSize
= m_win
->GetMinSize();
1006 if ( !minSize
.IsFullySpecified() )
1007 minSize
= m_win
->GetSize();
1018 // ----------------------------------------------------------------------------
1020 // ----------------------------------------------------------------------------
1022 // Default OnSize resets scrollbars, if any
1023 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
1025 if ( m_targetWindow
->GetAutoLayout() )
1027 wxSize size
= m_targetWindow
->GetBestVirtualSize();
1029 // This will call ::Layout() and ::AdjustScrollbars()
1030 m_win
->SetVirtualSize( size
);
1038 // This calls OnDraw, having adjusted the origin according to the current
1040 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
1042 // don't use m_targetWindow here, this is always called for ourselves
1043 wxPaintDC
dc(m_win
);
1049 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1050 // compatibility here - if we used OnKeyDown(), the programs which process
1051 // arrows themselves in their OnChar() would never get the message and like
1052 // this they always have the priority
1053 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
1055 int stx
= 0, sty
= 0, // view origin
1056 szx
= 0, szy
= 0, // view size (total)
1057 clix
= 0, cliy
= 0; // view size (on screen)
1059 GetViewStart(&stx
, &sty
);
1060 GetTargetSize(&clix
, &cliy
);
1061 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
1063 if( m_xScrollPixelsPerLine
)
1065 clix
/= m_xScrollPixelsPerLine
;
1066 szx
/= m_xScrollPixelsPerLine
;
1073 if( m_yScrollPixelsPerLine
)
1075 cliy
/= m_yScrollPixelsPerLine
;
1076 szy
/= m_yScrollPixelsPerLine
;
1084 int xScrollOld
= m_xScrollPosition
,
1085 yScrollOld
= m_yScrollPosition
;
1088 switch ( event
.GetKeyCode() )
1091 dsty
= sty
- (5 * cliy
/ 6);
1092 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1096 Scroll(-1, sty
+ (5 * cliy
/ 6));
1100 Scroll(0, event
.ControlDown() ? 0 : -1);
1104 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1108 Scroll(-1, sty
- 1);
1112 Scroll(-1, sty
+ 1);
1116 Scroll(stx
- 1, -1);
1120 Scroll(stx
+ 1, -1);
1128 if ( m_xScrollPosition
!= xScrollOld
)
1130 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1132 event
.SetEventObject(m_win
);
1133 m_win
->GetEventHandler()->ProcessEvent(event
);
1136 if ( m_yScrollPosition
!= yScrollOld
)
1138 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1140 event
.SetEventObject(m_win
);
1141 m_win
->GetEventHandler()->ProcessEvent(event
);
1145 // ----------------------------------------------------------------------------
1146 // autoscroll stuff: these functions deal with sending fake scroll events when
1147 // a captured mouse is being held outside the window
1148 // ----------------------------------------------------------------------------
1150 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1152 // only send the event if the window is scrollable in this direction
1153 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1154 return win
->HasScrollbar(event
.GetOrientation());
1157 void wxScrollHelper::StopAutoScrolling()
1160 if ( m_timerAutoScroll
)
1162 delete m_timerAutoScroll
;
1163 m_timerAutoScroll
= (wxTimer
*)NULL
;
1168 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1170 StopAutoScrolling();
1175 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1177 // don't prevent the usual processing of the event from taking place
1180 // when a captured mouse leave a scrolled window we start generate
1181 // scrolling events to allow, for example, extending selection beyond the
1182 // visible area in some controls
1183 if ( wxWindow::GetCapture() == m_targetWindow
)
1185 // where is the mouse leaving?
1187 wxPoint pt
= event
.GetPosition();
1190 orient
= wxHORIZONTAL
;
1193 else if ( pt
.y
< 0 )
1195 orient
= wxVERTICAL
;
1198 else // we're lower or to the right of the window
1200 wxSize size
= m_targetWindow
->GetClientSize();
1201 if ( pt
.x
> size
.x
)
1203 orient
= wxHORIZONTAL
;
1204 pos
= m_xScrollLines
;
1206 else if ( pt
.y
> size
.y
)
1208 orient
= wxVERTICAL
;
1209 pos
= m_yScrollLines
;
1211 else // this should be impossible
1213 // but seems to happen sometimes under wxMSW - maybe it's a bug
1214 // there but for now just ignore it
1216 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1222 // only start the auto scroll timer if the window can be scrolled in
1224 if ( !m_targetWindow
->HasScrollbar(orient
) )
1228 delete m_timerAutoScroll
;
1229 m_timerAutoScroll
= new wxAutoScrollTimer
1231 m_targetWindow
, this,
1232 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1233 : wxEVT_SCROLLWIN_LINEDOWN
,
1237 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1244 #if wxUSE_MOUSEWHEEL
1246 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1248 m_wheelRotation
+= event
.GetWheelRotation();
1249 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1250 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1255 wxScrollWinEvent newEvent
;
1257 newEvent
.SetPosition(0);
1258 newEvent
.SetOrientation(wxVERTICAL
);
1259 newEvent
.SetEventObject(m_win
);
1261 if (event
.IsPageScroll())
1264 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1266 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1268 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1272 lines
*= event
.GetLinesPerAction();
1274 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1276 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1278 int times
= abs(lines
);
1279 for (; times
> 0; times
--)
1280 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1285 #endif // wxUSE_MOUSEWHEEL
1287 // ----------------------------------------------------------------------------
1288 // wxScrolledWindow implementation
1289 // ----------------------------------------------------------------------------
1291 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
1293 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
1294 EVT_PAINT(wxScrolledWindow::OnPaint
)
1297 bool wxScrolledWindow::Create(wxWindow
*parent
,
1302 const wxString
& name
)
1304 m_targetWindow
= this;
1306 MacSetClipChildren( true ) ;
1309 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
1314 wxScrolledWindow::~wxScrolledWindow()
1318 void wxScrolledWindow::OnPaint(wxPaintEvent
& event
)
1320 // the user code didn't really draw the window if we got here, so set this
1321 // flag to try to call OnDraw() later
1322 m_handler
->ResetDrawnFlag();
1328 WXLRESULT
wxScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1332 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1335 // we need to process arrows ourselves for scrolling
1336 if ( nMsg
== WM_GETDLGCODE
)
1338 rc
|= DLGC_WANTARROWS
;