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"
44 #include "wx/recguard.h"
47 #include <windows.h> // for DLGC_WANTARROWS
48 #include "wx/msw/winundef.h"
52 // For wxRETAINED implementation
53 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
54 //This code switches off the compiler warnings
55 # pragma message disable nosimpint
59 # pragma message enable nosimpint
65 style wxHSCROLL | wxVSCROLL
68 // ----------------------------------------------------------------------------
69 // wxScrollHelperEvtHandler: intercept the events from the window and forward
70 // them to wxScrollHelper
71 // ----------------------------------------------------------------------------
73 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
76 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
78 m_scrollHelper
= scrollHelper
;
81 virtual bool ProcessEvent(wxEvent
& event
);
83 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
86 wxScrollHelper
*m_scrollHelper
;
88 bool m_hasDrawnWindow
;
90 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
94 // ----------------------------------------------------------------------------
95 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
96 // a captured mouse is held outside the window
97 // ----------------------------------------------------------------------------
99 class wxAutoScrollTimer
: public wxTimer
102 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
103 wxEventType eventTypeToSend
,
104 int pos
, int orient
);
106 virtual void Notify();
110 wxScrollHelper
*m_scrollHelper
;
111 wxEventType m_eventType
;
115 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
118 // ============================================================================
120 // ============================================================================
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
127 wxScrollHelper
*scroll
,
128 wxEventType eventTypeToSend
,
132 m_scrollHelper
= scroll
;
133 m_eventType
= eventTypeToSend
;
138 void wxAutoScrollTimer::Notify()
140 // only do all this as long as the window is capturing the mouse
141 if ( wxWindow::GetCapture() != m_win
)
145 else // we still capture the mouse, continue generating events
147 // first scroll the window if we are allowed to do it
148 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
149 event1
.SetEventObject(m_win
);
150 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
151 m_win
->GetEventHandler()->ProcessEvent(event1
) )
153 // and then send a pseudo mouse-move event to refresh the selection
154 wxMouseEvent
event2(wxEVT_MOTION
);
155 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
157 // the mouse event coordinates should be client, not screen as
158 // returned by wxGetMousePosition
159 wxWindow
*parentTop
= m_win
;
160 while ( parentTop
->GetParent() )
161 parentTop
= parentTop
->GetParent();
162 wxPoint ptOrig
= parentTop
->GetPosition();
163 event2
.m_x
-= ptOrig
.x
;
164 event2
.m_y
-= ptOrig
.y
;
166 event2
.SetEventObject(m_win
);
168 // FIXME: we don't fill in the other members - ok?
170 m_win
->GetEventHandler()->ProcessEvent(event2
);
172 else // can't scroll further, stop
180 // ----------------------------------------------------------------------------
181 // wxScrollHelperEvtHandler
182 // ----------------------------------------------------------------------------
184 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
186 wxEventType evType
= event
.GetEventType();
188 // the explanation of wxEVT_PAINT processing hack: for historic reasons
189 // there are 2 ways to process this event in classes deriving from
190 // wxScrolledWindow. The user code may
192 // 1. override wxScrolledWindow::OnDraw(dc)
193 // 2. define its own OnPaint() handler
195 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
196 // always processes the draw event, so we can't just try the window
197 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
198 // (the latter would never be called in wxUniversal).
200 // So the solution is to have a flag telling us whether the user code drew
201 // anything in the window. We set it to true here but reset it to false in
202 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
203 // user code defined OnPaint() in the derived class)
204 m_hasDrawnWindow
= true;
206 // pass it on to the real handler
207 bool processed
= wxEvtHandler::ProcessEvent(event
);
209 // always process the size events ourselves, even if the user code handles
210 // them as well, as we need to AdjustScrollbars()
212 // NB: it is important to do it after processing the event in the normal
213 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
214 // scrollbar[s] (dis)appear and it should be seen by the user code
216 if ( evType
== wxEVT_SIZE
)
218 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
225 // normally, nothing more to do here - except if it was a paint event
226 // which wasn't really processed, then we'll try to call our
227 // OnDraw() below (from HandleOnPaint)
228 if ( m_hasDrawnWindow
|| event
.IsCommandEvent() )
234 // reset the skipped flag to false as it might have been set to true in
235 // ProcessEvent() above
238 if ( evType
== wxEVT_PAINT
)
240 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
244 if ( evType
== wxEVT_SCROLLWIN_TOP
||
245 evType
== wxEVT_SCROLLWIN_BOTTOM
||
246 evType
== wxEVT_SCROLLWIN_LINEUP
||
247 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
248 evType
== wxEVT_SCROLLWIN_PAGEUP
||
249 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
250 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
251 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
253 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
254 return !event
.GetSkipped();
257 if ( evType
== wxEVT_ENTER_WINDOW
)
259 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
261 else if ( evType
== wxEVT_LEAVE_WINDOW
)
263 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
266 else if ( evType
== wxEVT_MOUSEWHEEL
)
268 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
270 #endif // wxUSE_MOUSEWHEEL
271 else if ( evType
== wxEVT_CHAR
)
273 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
274 return !event
.GetSkipped();
280 // ----------------------------------------------------------------------------
281 // wxScrollHelper construction
282 // ----------------------------------------------------------------------------
284 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
286 wxASSERT_MSG( win
, _T("associated window can't be NULL in wxScrollHelper") );
288 m_xScrollPixelsPerLine
=
289 m_yScrollPixelsPerLine
=
294 m_xScrollLinesPerPage
=
295 m_yScrollLinesPerPage
= 0;
297 m_xScrollingEnabled
=
298 m_yScrollingEnabled
= true;
307 m_targetWindow
= (wxWindow
*)NULL
;
309 m_timerAutoScroll
= (wxTimer
*)NULL
;
315 // by default, the associated window is also the target window
316 DoSetTargetWindow(win
);
319 wxScrollHelper::~wxScrollHelper()
326 // ----------------------------------------------------------------------------
327 // setting scrolling parameters
328 // ----------------------------------------------------------------------------
330 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
340 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
343 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
344 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
346 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
347 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
348 (xPos
!= m_xScrollPosition
) ||
349 (yPos
!= m_yScrollPosition
)
352 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
353 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
354 m_xScrollPosition
= xPos
;
355 m_yScrollPosition
= yPos
;
357 int w
= noUnitsX
* pixelsPerUnitX
;
358 int h
= noUnitsY
* pixelsPerUnitY
;
360 // For better backward compatibility we set persisting limits
361 // here not just the size. It makes SetScrollbars 'sticky'
362 // emulating the old non-autoscroll behaviour.
363 // m_targetWindow->SetVirtualSizeHints( w, h );
365 // The above should arguably be deprecated, this however we still need.
367 // take care not to set 0 virtual size, 0 means that we don't have any
368 // scrollbars and hence we should use the real size instead of the virtual
369 // one which is indicated by using wxDefaultCoord
370 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
371 h
? h
: wxDefaultCoord
);
373 if (do_refresh
&& !noRefresh
)
374 m_targetWindow
->Refresh(true, GetScrollRect());
376 #ifndef __WXUNIVERSAL__
377 // If the target is not the same as the window with the scrollbars,
378 // then we need to update the scrollbars here, since they won't have
379 // been updated by SetVirtualSize().
380 if ( m_targetWindow
!= m_win
)
381 #endif // !__WXUNIVERSAL__
385 #ifndef __WXUNIVERSAL__
388 // otherwise this has been done by AdjustScrollbars, above
390 #endif // !__WXUNIVERSAL__
393 // ----------------------------------------------------------------------------
394 // [target] window handling
395 // ----------------------------------------------------------------------------
397 void wxScrollHelper::DeleteEvtHandler()
399 // search for m_handler in the handler list
400 if ( m_win
&& m_handler
)
402 if ( m_win
->RemoveEventHandler(m_handler
) )
406 //else: something is very wrong, so better [maybe] leak memory than
407 // risk a crash because of double deletion
413 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
415 m_targetWindow
= target
;
417 target
->MacSetClipChildren( true ) ;
420 // install the event handler which will intercept the events we're
421 // interested in (but only do it for our real window, not the target window
422 // which we scroll - we don't need to hijack its events)
423 if ( m_targetWindow
== m_win
)
425 // if we already have a handler, delete it first
428 m_handler
= new wxScrollHelperEvtHandler(this);
429 m_targetWindow
->PushEventHandler(m_handler
);
433 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
435 wxCHECK_RET( target
, wxT("target window must not be NULL") );
437 if ( target
== m_targetWindow
)
440 DoSetTargetWindow(target
);
443 wxWindow
*wxScrollHelper::GetTargetWindow() const
445 return m_targetWindow
;
448 // ----------------------------------------------------------------------------
449 // scrolling implementation itself
450 // ----------------------------------------------------------------------------
452 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
454 int nScrollInc
= CalcScrollInc(event
);
455 if ( nScrollInc
== 0 )
457 // can't scroll further
463 int orient
= event
.GetOrientation();
464 if (orient
== wxHORIZONTAL
)
466 m_xScrollPosition
+= nScrollInc
;
467 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
471 m_yScrollPosition
+= nScrollInc
;
472 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
475 bool needsRefresh
= false;
478 if (orient
== wxHORIZONTAL
)
480 if ( m_xScrollingEnabled
)
482 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
491 if ( m_yScrollingEnabled
)
493 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
503 m_targetWindow
->Refresh(true, GetScrollRect());
507 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
511 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
513 int pos
= event
.GetPosition();
514 int orient
= event
.GetOrientation();
517 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
519 if (orient
== wxHORIZONTAL
)
520 nScrollInc
= - m_xScrollPosition
;
522 nScrollInc
= - m_yScrollPosition
;
524 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
526 if (orient
== wxHORIZONTAL
)
527 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
529 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
531 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
535 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
539 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
541 if (orient
== wxHORIZONTAL
)
542 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
544 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
546 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
548 if (orient
== wxHORIZONTAL
)
549 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
551 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
553 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
554 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
556 if (orient
== wxHORIZONTAL
)
557 nScrollInc
= pos
- m_xScrollPosition
;
559 nScrollInc
= pos
- m_yScrollPosition
;
562 if (orient
== wxHORIZONTAL
)
564 if (m_xScrollPixelsPerLine
> 0)
566 if ( m_xScrollPosition
+ nScrollInc
< 0 )
568 // As -ve as we can go
569 nScrollInc
= -m_xScrollPosition
;
571 else // check for the other bound
573 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
574 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
576 // As +ve as we can go
577 nScrollInc
= posMax
- m_xScrollPosition
;
582 m_targetWindow
->Refresh(true, GetScrollRect());
586 if ( m_yScrollPixelsPerLine
> 0 )
588 if ( m_yScrollPosition
+ nScrollInc
< 0 )
590 // As -ve as we can go
591 nScrollInc
= -m_yScrollPosition
;
593 else // check for the other bound
595 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
596 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
598 // As +ve as we can go
599 nScrollInc
= posMax
- m_yScrollPosition
;
605 // VZ: why do we do this? (FIXME)
606 m_targetWindow
->Refresh(true, GetScrollRect());
613 // Adjust the scrollbars - new version.
614 void wxScrollHelper::AdjustScrollbars()
616 static wxRecursionGuardFlag s_flagReentrancy
;
617 wxRecursionGuard
guard(s_flagReentrancy
);
618 if ( guard
.IsInside() )
620 // don't reenter AdjustScrollbars() while another call to
621 // AdjustScrollbars() is in progress because this may lead to calling
622 // ScrollWindow() twice and this can really happen under MSW if
623 // SetScrollbar() call below adds or removes the scrollbar which
624 // changes the window size and hence results in another
625 // AdjustScrollbars() call
632 int oldXScroll
= m_xScrollPosition
;
633 int oldYScroll
= m_yScrollPosition
;
635 // VZ: at least under Windows this loop is useless because when scrollbars
636 // [dis]appear we get a WM_SIZE resulting in another call to
637 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
638 // it here for now but it would be better to ensure that all ports
639 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
640 // necessary, and remove it later
641 // JACS: Stop potential infinite loop by limiting number of iterations
642 int iterationCount
= 0;
643 const int iterationMax
= 5;
648 GetTargetSize(&w
, 0);
650 // scroll lines per page: if 0, no scrolling is needed
653 if ( m_xScrollPixelsPerLine
== 0 )
655 // scrolling is disabled
657 m_xScrollPosition
= 0;
660 else // might need scrolling
662 // Round up integer division to catch any "leftover" client space.
663 const int wVirt
= m_targetWindow
->GetVirtualSize().GetWidth();
664 m_xScrollLines
= (wVirt
+ m_xScrollPixelsPerLine
- 1) / m_xScrollPixelsPerLine
;
666 // Calculate page size i.e. number of scroll units you get on the
667 // current client window.
668 linesPerPage
= w
/ m_xScrollPixelsPerLine
;
670 // Special case. When client and virtual size are very close but
671 // the client is big enough, kill scrollbar.
672 if ((linesPerPage
< m_xScrollLines
) && (w
>= wVirt
)) ++linesPerPage
;
674 if (linesPerPage
>= m_xScrollLines
)
676 // we're big enough to not need scrolling
679 m_xScrollPosition
= 0;
681 else // we do need a scrollbar
683 if ( linesPerPage
< 1 )
686 // Correct position if greater than extent of canvas minus
687 // the visible portion of it or if below zero
688 const int posMax
= m_xScrollLines
- linesPerPage
;
689 if ( m_xScrollPosition
> posMax
)
690 m_xScrollPosition
= posMax
;
691 else if ( m_xScrollPosition
< 0 )
692 m_xScrollPosition
= 0;
696 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
,
697 linesPerPage
, m_xScrollLines
);
699 // The amount by which we scroll when paging
700 SetScrollPageSize(wxHORIZONTAL
, linesPerPage
);
702 GetTargetSize(0, &h
);
704 if ( m_yScrollPixelsPerLine
== 0 )
706 // scrolling is disabled
708 m_yScrollPosition
= 0;
711 else // might need scrolling
713 // Round up integer division to catch any "leftover" client space.
714 const int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
715 m_yScrollLines
= ( hVirt
+ m_yScrollPixelsPerLine
- 1 ) / m_yScrollPixelsPerLine
;
717 // Calculate page size i.e. number of scroll units you get on the
718 // current client window.
719 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
721 // Special case. When client and virtual size are very close but
722 // the client is big enough, kill scrollbar.
723 if ((linesPerPage
< m_yScrollLines
) && (h
>= hVirt
)) ++linesPerPage
;
725 if (linesPerPage
>= m_yScrollLines
)
727 // we're big enough to not need scrolling
730 m_yScrollPosition
= 0;
732 else // we do need a scrollbar
734 if ( linesPerPage
< 1 )
737 // Correct position if greater than extent of canvas minus
738 // the visible portion of it or if below zero
739 const int posMax
= m_yScrollLines
- linesPerPage
;
740 if ( m_yScrollPosition
> posMax
)
741 m_yScrollPosition
= posMax
;
742 else if ( m_yScrollPosition
< 0 )
743 m_yScrollPosition
= 0;
747 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
748 linesPerPage
, m_yScrollLines
);
750 // The amount by which we scroll when paging
751 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
754 // If a scrollbar (dis)appeared as a result of this, adjust them again.
758 GetTargetSize( &w
, &h
);
759 } while ( (w
!= oldw
|| h
!= oldh
) && (iterationCount
< iterationMax
) );
762 // Sorry, some Motif-specific code to implement a backing pixmap
763 // for the wxRETAINED style. Implementing a backing store can't
764 // be entirely generic because it relies on the wxWindowDC implementation
765 // to duplicate X drawing calls for the backing pixmap.
767 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
769 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
771 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
772 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
773 if (m_targetWindow
->GetBackingPixmap() &&
774 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
775 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
777 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
778 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
781 if (!m_targetWindow
->GetBackingPixmap() &&
782 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
784 int depth
= wxDisplayDepth();
785 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
786 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
787 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
788 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
794 if (oldXScroll
!= m_xScrollPosition
)
796 if (m_xScrollingEnabled
)
797 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
800 m_targetWindow
->Refresh(true, GetScrollRect());
803 if (oldYScroll
!= m_yScrollPosition
)
805 if (m_yScrollingEnabled
)
806 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
809 m_targetWindow
->Refresh(true, GetScrollRect());
813 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
815 wxPoint pt
= dc
.GetDeviceOrigin();
816 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
817 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
818 dc
.SetUserScale( m_scaleX
, m_scaleY
);
821 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
823 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
824 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
826 m_xScrollPixelsPerLine
= xstep
;
827 m_yScrollPixelsPerLine
= ystep
;
829 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
830 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
832 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
833 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
834 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
839 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
842 *x_unit
= m_xScrollPixelsPerLine
;
844 *y_unit
= m_yScrollPixelsPerLine
;
847 int wxScrollHelper::GetScrollPageSize(int orient
) const
849 if ( orient
== wxHORIZONTAL
)
850 return m_xScrollLinesPerPage
;
852 return m_yScrollLinesPerPage
;
855 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
857 if ( orient
== wxHORIZONTAL
)
858 m_xScrollLinesPerPage
= pageSize
;
860 m_yScrollLinesPerPage
= pageSize
;
864 * Scroll to given position (scroll position, not pixel position)
866 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
871 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
872 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
875 GetTargetSize(&w
, &h
);
877 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
879 int old_x
= m_xScrollPosition
;
880 m_xScrollPosition
= x_pos
;
882 // Calculate page size i.e. number of scroll units you get on the
883 // current client window
884 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
885 if (noPagePositions
< 1) noPagePositions
= 1;
887 // Correct position if greater than extent of canvas minus
888 // the visible portion of it or if below zero
889 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
890 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
892 if (old_x
!= m_xScrollPosition
) {
893 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
894 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
898 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
900 int old_y
= m_yScrollPosition
;
901 m_yScrollPosition
= y_pos
;
903 // Calculate page size i.e. number of scroll units you get on the
904 // current client window
905 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
906 if (noPagePositions
< 1) noPagePositions
= 1;
908 // Correct position if greater than extent of canvas minus
909 // the visible portion of it or if below zero
910 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
911 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
913 if (old_y
!= m_yScrollPosition
) {
914 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
915 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
921 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
923 m_xScrollingEnabled
= x_scroll
;
924 m_yScrollingEnabled
= y_scroll
;
927 // Where the current view starts from
928 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
931 *x
= m_xScrollPosition
;
933 *y
= m_yScrollPosition
;
936 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
939 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
941 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
944 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
947 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
949 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
952 // ----------------------------------------------------------------------------
954 // ----------------------------------------------------------------------------
956 bool wxScrollHelper::ScrollLayout()
958 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
960 // If we're the scroll target, take into account the
961 // virtual size and scrolled position of the window.
963 int x
= 0, y
= 0, w
= 0, h
= 0;
964 CalcScrolledPosition(0,0, &x
,&y
);
965 m_win
->GetVirtualSize(&w
, &h
);
966 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
970 // fall back to default for LayoutConstraints
971 return m_win
->wxWindow::Layout();
974 void wxScrollHelper::ScrollDoSetVirtualSize(int x
, int y
)
976 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
979 if (m_win
->GetAutoLayout())
983 // wxWindow's GetBestVirtualSize returns the actual window size,
984 // whereas we want to return the virtual size
985 wxSize
wxScrollHelper::ScrollGetBestVirtualSize() const
987 wxSize
clientSize(m_win
->GetClientSize());
988 if ( m_win
->GetSizer() )
989 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
994 // return the window best size from the given best virtual size
996 wxScrollHelper::ScrollGetWindowSizeForVirtualSize(const wxSize
& size
) const
998 // Only use the content to set the window size in the direction
999 // where there's no scrolling; otherwise we're going to get a huge
1000 // window in the direction in which scrolling is enabled
1002 GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1004 wxSize minSize
= m_win
->GetMinSize();
1005 if ( !minSize
.IsFullySpecified() )
1006 minSize
= m_win
->GetSize();
1017 // ----------------------------------------------------------------------------
1019 // ----------------------------------------------------------------------------
1021 // Default OnSize resets scrollbars, if any
1022 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
1024 if ( m_targetWindow
->GetAutoLayout() )
1026 wxSize size
= m_targetWindow
->GetBestVirtualSize();
1028 // This will call ::Layout() and ::AdjustScrollbars()
1029 m_win
->SetVirtualSize( size
);
1037 // This calls OnDraw, having adjusted the origin according to the current
1039 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
1041 // don't use m_targetWindow here, this is always called for ourselves
1042 wxPaintDC
dc(m_win
);
1048 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1049 // compatibility here - if we used OnKeyDown(), the programs which process
1050 // arrows themselves in their OnChar() would never get the message and like
1051 // this they always have the priority
1052 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
1054 int stx
= 0, sty
= 0, // view origin
1055 szx
= 0, szy
= 0, // view size (total)
1056 clix
= 0, cliy
= 0; // view size (on screen)
1058 GetViewStart(&stx
, &sty
);
1059 GetTargetSize(&clix
, &cliy
);
1060 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
1062 if( m_xScrollPixelsPerLine
)
1064 clix
/= m_xScrollPixelsPerLine
;
1065 szx
/= m_xScrollPixelsPerLine
;
1072 if( m_yScrollPixelsPerLine
)
1074 cliy
/= m_yScrollPixelsPerLine
;
1075 szy
/= m_yScrollPixelsPerLine
;
1083 int xScrollOld
= m_xScrollPosition
,
1084 yScrollOld
= m_yScrollPosition
;
1087 switch ( event
.GetKeyCode() )
1090 dsty
= sty
- (5 * cliy
/ 6);
1091 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1095 Scroll(-1, sty
+ (5 * cliy
/ 6));
1099 Scroll(0, event
.ControlDown() ? 0 : -1);
1103 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1107 Scroll(-1, sty
- 1);
1111 Scroll(-1, sty
+ 1);
1115 Scroll(stx
- 1, -1);
1119 Scroll(stx
+ 1, -1);
1127 if ( m_xScrollPosition
!= xScrollOld
)
1129 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1131 event
.SetEventObject(m_win
);
1132 m_win
->GetEventHandler()->ProcessEvent(event
);
1135 if ( m_yScrollPosition
!= yScrollOld
)
1137 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1139 event
.SetEventObject(m_win
);
1140 m_win
->GetEventHandler()->ProcessEvent(event
);
1144 // ----------------------------------------------------------------------------
1145 // autoscroll stuff: these functions deal with sending fake scroll events when
1146 // a captured mouse is being held outside the window
1147 // ----------------------------------------------------------------------------
1149 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1151 // only send the event if the window is scrollable in this direction
1152 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1153 return win
->HasScrollbar(event
.GetOrientation());
1156 void wxScrollHelper::StopAutoScrolling()
1159 if ( m_timerAutoScroll
)
1161 delete m_timerAutoScroll
;
1162 m_timerAutoScroll
= (wxTimer
*)NULL
;
1167 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1169 StopAutoScrolling();
1174 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1176 // don't prevent the usual processing of the event from taking place
1179 // when a captured mouse leave a scrolled window we start generate
1180 // scrolling events to allow, for example, extending selection beyond the
1181 // visible area in some controls
1182 if ( wxWindow::GetCapture() == m_targetWindow
)
1184 // where is the mouse leaving?
1186 wxPoint pt
= event
.GetPosition();
1189 orient
= wxHORIZONTAL
;
1192 else if ( pt
.y
< 0 )
1194 orient
= wxVERTICAL
;
1197 else // we're lower or to the right of the window
1199 wxSize size
= m_targetWindow
->GetClientSize();
1200 if ( pt
.x
> size
.x
)
1202 orient
= wxHORIZONTAL
;
1203 pos
= m_xScrollLines
;
1205 else if ( pt
.y
> size
.y
)
1207 orient
= wxVERTICAL
;
1208 pos
= m_yScrollLines
;
1210 else // this should be impossible
1212 // but seems to happen sometimes under wxMSW - maybe it's a bug
1213 // there but for now just ignore it
1215 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1221 // only start the auto scroll timer if the window can be scrolled in
1223 if ( !m_targetWindow
->HasScrollbar(orient
) )
1227 delete m_timerAutoScroll
;
1228 m_timerAutoScroll
= new wxAutoScrollTimer
1230 m_targetWindow
, this,
1231 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1232 : wxEVT_SCROLLWIN_LINEDOWN
,
1236 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1243 #if wxUSE_MOUSEWHEEL
1245 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1247 m_wheelRotation
+= event
.GetWheelRotation();
1248 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1249 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1254 wxScrollWinEvent newEvent
;
1256 newEvent
.SetPosition(0);
1257 newEvent
.SetOrientation(wxVERTICAL
);
1258 newEvent
.SetEventObject(m_win
);
1260 if (event
.IsPageScroll())
1263 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1265 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1267 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1271 lines
*= event
.GetLinesPerAction();
1273 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1275 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1277 int times
= abs(lines
);
1278 for (; times
> 0; times
--)
1279 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1284 #endif // wxUSE_MOUSEWHEEL
1286 // ----------------------------------------------------------------------------
1287 // wxScrolledWindow implementation
1288 // ----------------------------------------------------------------------------
1290 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
1292 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
1293 EVT_PAINT(wxScrolledWindow::OnPaint
)
1296 bool wxScrolledWindow::Create(wxWindow
*parent
,
1301 const wxString
& name
)
1303 m_targetWindow
= this;
1305 MacSetClipChildren( true ) ;
1308 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
1313 wxScrolledWindow::~wxScrolledWindow()
1317 void wxScrolledWindow::OnPaint(wxPaintEvent
& event
)
1319 // the user code didn't really draw the window if we got here, so set this
1320 // flag to try to call OnDraw() later
1321 m_handler
->ResetDrawnFlag();
1327 WXLRESULT
wxScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1331 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1334 // we need to process arrows ourselves for scrolling
1335 if ( nMsg
== WM_GETDLGCODE
)
1337 rc
|= DLGC_WANTARROWS
;