1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/scrolwin.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"
33 #include "wx/dcclient.h"
35 #include "wx/scrolwin.h"
41 #include "wx/recguard.h"
44 #include <windows.h> // for DLGC_WANTARROWS
45 #include "wx/msw/winundef.h"
49 // For wxRETAINED implementation
50 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
51 //This code switches off the compiler warnings
52 # pragma message disable nosimpint
56 # pragma message enable nosimpint
62 style wxHSCROLL | wxVSCROLL
65 // ----------------------------------------------------------------------------
66 // wxScrollHelperEvtHandler: intercept the events from the window and forward
67 // them to wxScrollHelper
68 // ----------------------------------------------------------------------------
70 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
73 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
75 m_scrollHelper
= scrollHelper
;
78 virtual bool ProcessEvent(wxEvent
& event
);
80 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
83 wxScrollHelper
*m_scrollHelper
;
85 bool m_hasDrawnWindow
;
87 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
91 // ----------------------------------------------------------------------------
92 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
93 // a captured mouse is held outside the window
94 // ----------------------------------------------------------------------------
96 class wxAutoScrollTimer
: public wxTimer
99 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
100 wxEventType eventTypeToSend
,
101 int pos
, int orient
);
103 virtual void Notify();
107 wxScrollHelper
*m_scrollHelper
;
108 wxEventType m_eventType
;
112 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
124 wxScrollHelper
*scroll
,
125 wxEventType eventTypeToSend
,
129 m_scrollHelper
= scroll
;
130 m_eventType
= eventTypeToSend
;
135 void wxAutoScrollTimer::Notify()
137 // only do all this as long as the window is capturing the mouse
138 if ( wxWindow::GetCapture() != m_win
)
142 else // we still capture the mouse, continue generating events
144 // first scroll the window if we are allowed to do it
145 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
146 event1
.SetEventObject(m_win
);
147 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
148 m_win
->GetEventHandler()->ProcessEvent(event1
) )
150 // and then send a pseudo mouse-move event to refresh the selection
151 wxMouseEvent
event2(wxEVT_MOTION
);
152 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
154 // the mouse event coordinates should be client, not screen as
155 // returned by wxGetMousePosition
156 wxWindow
*parentTop
= m_win
;
157 while ( parentTop
->GetParent() )
158 parentTop
= parentTop
->GetParent();
159 wxPoint ptOrig
= parentTop
->GetPosition();
160 event2
.m_x
-= ptOrig
.x
;
161 event2
.m_y
-= ptOrig
.y
;
163 event2
.SetEventObject(m_win
);
165 // FIXME: we don't fill in the other members - ok?
167 m_win
->GetEventHandler()->ProcessEvent(event2
);
169 else // can't scroll further, stop
177 // ----------------------------------------------------------------------------
178 // wxScrollHelperEvtHandler
179 // ----------------------------------------------------------------------------
181 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
183 wxEventType evType
= event
.GetEventType();
185 // the explanation of wxEVT_PAINT processing hack: for historic reasons
186 // there are 2 ways to process this event in classes deriving from
187 // wxScrolledWindow. The user code may
189 // 1. override wxScrolledWindow::OnDraw(dc)
190 // 2. define its own OnPaint() handler
192 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
193 // always processes the draw event, so we can't just try the window
194 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
195 // (the latter would never be called in wxUniversal).
197 // So the solution is to have a flag telling us whether the user code drew
198 // anything in the window. We set it to true here but reset it to false in
199 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
200 // user code defined OnPaint() in the derived class)
201 m_hasDrawnWindow
= true;
203 // pass it on to the real handler
204 bool processed
= wxEvtHandler::ProcessEvent(event
);
206 // always process the size events ourselves, even if the user code handles
207 // them as well, as we need to AdjustScrollbars()
209 // NB: it is important to do it after processing the event in the normal
210 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
211 // scrollbar[s] (dis)appear and it should be seen by the user code
213 if ( evType
== wxEVT_SIZE
)
215 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
222 // normally, nothing more to do here - except if it was a paint event
223 // which wasn't really processed, then we'll try to call our
224 // OnDraw() below (from HandleOnPaint)
225 if ( m_hasDrawnWindow
)
231 // reset the skipped flag to false as it might have been set to true in
232 // ProcessEvent() above
235 if ( evType
== wxEVT_PAINT
)
237 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
241 if ( evType
== wxEVT_SCROLLWIN_TOP
||
242 evType
== wxEVT_SCROLLWIN_BOTTOM
||
243 evType
== wxEVT_SCROLLWIN_LINEUP
||
244 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
245 evType
== wxEVT_SCROLLWIN_PAGEUP
||
246 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
247 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
248 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
250 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
251 return !event
.GetSkipped();
254 if ( evType
== wxEVT_ENTER_WINDOW
)
256 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
258 else if ( evType
== wxEVT_LEAVE_WINDOW
)
260 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
263 else if ( evType
== wxEVT_MOUSEWHEEL
)
265 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
267 #endif // wxUSE_MOUSEWHEEL
268 else if ( evType
== wxEVT_CHAR
)
270 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
271 return !event
.GetSkipped();
277 // ----------------------------------------------------------------------------
278 // wxScrollHelper construction
279 // ----------------------------------------------------------------------------
281 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
283 wxASSERT_MSG( win
, _T("associated window can't be NULL in wxScrollHelper") );
285 m_xScrollPixelsPerLine
=
286 m_yScrollPixelsPerLine
=
291 m_xScrollLinesPerPage
=
292 m_yScrollLinesPerPage
= 0;
294 m_xScrollingEnabled
=
295 m_yScrollingEnabled
= true;
304 m_targetWindow
= (wxWindow
*)NULL
;
306 m_timerAutoScroll
= (wxTimer
*)NULL
;
312 // by default, the associated window is also the target window
313 DoSetTargetWindow(win
);
316 wxScrollHelper::~wxScrollHelper()
323 // ----------------------------------------------------------------------------
324 // setting scrolling parameters
325 // ----------------------------------------------------------------------------
327 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
337 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
340 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
341 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
343 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
344 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
345 (xPos
!= m_xScrollPosition
) ||
346 (yPos
!= m_yScrollPosition
)
349 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
350 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
351 m_xScrollPosition
= xPos
;
352 m_yScrollPosition
= yPos
;
354 int w
= noUnitsX
* pixelsPerUnitX
;
355 int h
= noUnitsY
* pixelsPerUnitY
;
357 // For better backward compatibility we set persisting limits
358 // here not just the size. It makes SetScrollbars 'sticky'
359 // emulating the old non-autoscroll behaviour.
360 // m_targetWindow->SetVirtualSizeHints( w, h );
362 // The above should arguably be deprecated, this however we still need.
364 // take care not to set 0 virtual size, 0 means that we don't have any
365 // scrollbars and hence we should use the real size instead of the virtual
366 // one which is indicated by using wxDefaultCoord
367 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
368 h
? h
: wxDefaultCoord
);
370 if (do_refresh
&& !noRefresh
)
371 m_targetWindow
->Refresh(true, GetScrollRect());
373 #ifndef __WXUNIVERSAL__
374 // If the target is not the same as the window with the scrollbars,
375 // then we need to update the scrollbars here, since they won't have
376 // been updated by SetVirtualSize().
377 if ( m_targetWindow
!= m_win
)
378 #endif // !__WXUNIVERSAL__
382 #ifndef __WXUNIVERSAL__
385 // otherwise this has been done by AdjustScrollbars, above
387 #endif // !__WXUNIVERSAL__
390 // ----------------------------------------------------------------------------
391 // [target] window handling
392 // ----------------------------------------------------------------------------
394 void wxScrollHelper::DeleteEvtHandler()
396 // search for m_handler in the handler list
397 if ( m_win
&& m_handler
)
399 if ( m_win
->RemoveEventHandler(m_handler
) )
403 //else: something is very wrong, so better [maybe] leak memory than
404 // risk a crash because of double deletion
410 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
412 m_targetWindow
= target
;
414 target
->MacSetClipChildren( true ) ;
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 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
510 int pos
= event
.GetPosition();
511 int orient
= event
.GetOrientation();
514 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
516 if (orient
== wxHORIZONTAL
)
517 nScrollInc
= - m_xScrollPosition
;
519 nScrollInc
= - m_yScrollPosition
;
521 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
523 if (orient
== wxHORIZONTAL
)
524 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
526 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
528 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
532 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
536 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
538 if (orient
== wxHORIZONTAL
)
539 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
541 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
543 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
545 if (orient
== wxHORIZONTAL
)
546 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
548 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
550 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
551 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
553 if (orient
== wxHORIZONTAL
)
554 nScrollInc
= pos
- m_xScrollPosition
;
556 nScrollInc
= pos
- m_yScrollPosition
;
559 if (orient
== wxHORIZONTAL
)
561 if (m_xScrollPixelsPerLine
> 0)
563 if ( m_xScrollPosition
+ nScrollInc
< 0 )
565 // As -ve as we can go
566 nScrollInc
= -m_xScrollPosition
;
568 else // check for the other bound
570 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
571 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
573 // As +ve as we can go
574 nScrollInc
= posMax
- m_xScrollPosition
;
579 m_targetWindow
->Refresh(true, GetScrollRect());
583 if ( m_yScrollPixelsPerLine
> 0 )
585 if ( m_yScrollPosition
+ nScrollInc
< 0 )
587 // As -ve as we can go
588 nScrollInc
= -m_yScrollPosition
;
590 else // check for the other bound
592 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
593 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
595 // As +ve as we can go
596 nScrollInc
= posMax
- m_yScrollPosition
;
602 // VZ: why do we do this? (FIXME)
603 m_targetWindow
->Refresh(true, GetScrollRect());
610 // Adjust the scrollbars - new version.
611 void wxScrollHelper::AdjustScrollbars()
613 static wxRecursionGuardFlag s_flagReentrancy
;
614 wxRecursionGuard
guard(s_flagReentrancy
);
615 if ( guard
.IsInside() )
617 // don't reenter AdjustScrollbars() while another call to
618 // AdjustScrollbars() is in progress because this may lead to calling
619 // ScrollWindow() twice and this can really happen under MSW if
620 // SetScrollbar() call below adds or removes the scrollbar which
621 // changes the window size and hence results in another
622 // AdjustScrollbars() call
629 int oldXScroll
= m_xScrollPosition
;
630 int oldYScroll
= m_yScrollPosition
;
632 // VZ: at least under Windows this loop is useless because when scrollbars
633 // [dis]appear we get a WM_SIZE resulting in another call to
634 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
635 // it here for now but it would be better to ensure that all ports
636 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
637 // necessary, and remove it later
638 // JACS: Stop potential infinite loop by limiting number of iterations
639 int iterationCount
= 0;
640 const int iterationMax
= 5;
645 GetTargetSize(&w
, 0);
647 // scroll lines per page: if 0, no scrolling is needed
650 if ( m_xScrollPixelsPerLine
== 0 )
652 // scrolling is disabled
654 m_xScrollPosition
= 0;
657 else // might need scrolling
659 // Round up integer division to catch any "leftover" client space.
660 const int wVirt
= m_targetWindow
->GetVirtualSize().GetWidth();
661 m_xScrollLines
= (wVirt
+ m_xScrollPixelsPerLine
- 1) / m_xScrollPixelsPerLine
;
663 // Calculate page size i.e. number of scroll units you get on the
664 // current client window.
665 linesPerPage
= w
/ m_xScrollPixelsPerLine
;
667 // Special case. When client and virtual size are very close but
668 // the client is big enough, kill scrollbar.
669 if ((linesPerPage
< m_xScrollLines
) && (w
>= wVirt
)) ++linesPerPage
;
671 if (linesPerPage
>= m_xScrollLines
)
673 // we're big enough to not need scrolling
676 m_xScrollPosition
= 0;
678 else // we do need a scrollbar
680 if ( linesPerPage
< 1 )
683 // Correct position if greater than extent of canvas minus
684 // the visible portion of it or if below zero
685 const int posMax
= m_xScrollLines
- linesPerPage
;
686 if ( m_xScrollPosition
> posMax
)
687 m_xScrollPosition
= posMax
;
688 else if ( m_xScrollPosition
< 0 )
689 m_xScrollPosition
= 0;
693 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
,
694 linesPerPage
, m_xScrollLines
);
696 // The amount by which we scroll when paging
697 SetScrollPageSize(wxHORIZONTAL
, linesPerPage
);
699 GetTargetSize(0, &h
);
701 if ( m_yScrollPixelsPerLine
== 0 )
703 // scrolling is disabled
705 m_yScrollPosition
= 0;
708 else // might need scrolling
710 // Round up integer division to catch any "leftover" client space.
711 const int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
712 m_yScrollLines
= ( hVirt
+ m_yScrollPixelsPerLine
- 1 ) / m_yScrollPixelsPerLine
;
714 // Calculate page size i.e. number of scroll units you get on the
715 // current client window.
716 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
718 // Special case. When client and virtual size are very close but
719 // the client is big enough, kill scrollbar.
720 if ((linesPerPage
< m_yScrollLines
) && (h
>= hVirt
)) ++linesPerPage
;
722 if (linesPerPage
>= m_yScrollLines
)
724 // we're big enough to not need scrolling
727 m_yScrollPosition
= 0;
729 else // we do need a scrollbar
731 if ( linesPerPage
< 1 )
734 // Correct position if greater than extent of canvas minus
735 // the visible portion of it or if below zero
736 const int posMax
= m_yScrollLines
- linesPerPage
;
737 if ( m_yScrollPosition
> posMax
)
738 m_yScrollPosition
= posMax
;
739 else if ( m_yScrollPosition
< 0 )
740 m_yScrollPosition
= 0;
744 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
745 linesPerPage
, m_yScrollLines
);
747 // The amount by which we scroll when paging
748 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
751 // If a scrollbar (dis)appeared as a result of this, adjust them again.
755 GetTargetSize( &w
, &h
);
756 } while ( (w
!= oldw
|| h
!= oldh
) && (iterationCount
< iterationMax
) );
759 // Sorry, some Motif-specific code to implement a backing pixmap
760 // for the wxRETAINED style. Implementing a backing store can't
761 // be entirely generic because it relies on the wxWindowDC implementation
762 // to duplicate X drawing calls for the backing pixmap.
764 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
766 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
768 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
769 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
770 if (m_targetWindow
->GetBackingPixmap() &&
771 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
772 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
774 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
775 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
778 if (!m_targetWindow
->GetBackingPixmap() &&
779 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
781 int depth
= wxDisplayDepth();
782 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
783 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
784 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
785 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
791 if (oldXScroll
!= m_xScrollPosition
)
793 if (m_xScrollingEnabled
)
794 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
797 m_targetWindow
->Refresh(true, GetScrollRect());
800 if (oldYScroll
!= m_yScrollPosition
)
802 if (m_yScrollingEnabled
)
803 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
806 m_targetWindow
->Refresh(true, GetScrollRect());
810 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
812 wxPoint pt
= dc
.GetDeviceOrigin();
813 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
814 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
815 dc
.SetUserScale( m_scaleX
, m_scaleY
);
818 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
820 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
821 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
823 m_xScrollPixelsPerLine
= xstep
;
824 m_yScrollPixelsPerLine
= ystep
;
826 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
827 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
829 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
830 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
831 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
836 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
839 *x_unit
= m_xScrollPixelsPerLine
;
841 *y_unit
= m_yScrollPixelsPerLine
;
844 int wxScrollHelper::GetScrollPageSize(int orient
) const
846 if ( orient
== wxHORIZONTAL
)
847 return m_xScrollLinesPerPage
;
849 return m_yScrollLinesPerPage
;
852 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
854 if ( orient
== wxHORIZONTAL
)
855 m_xScrollLinesPerPage
= pageSize
;
857 m_yScrollLinesPerPage
= pageSize
;
861 * Scroll to given position (scroll position, not pixel position)
863 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
868 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
869 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
872 GetTargetSize(&w
, &h
);
874 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
876 int old_x
= m_xScrollPosition
;
877 m_xScrollPosition
= x_pos
;
879 // Calculate page size i.e. number of scroll units you get on the
880 // current client window
881 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
882 if (noPagePositions
< 1) noPagePositions
= 1;
884 // Correct position if greater than extent of canvas minus
885 // the visible portion of it or if below zero
886 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
887 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
889 if (old_x
!= m_xScrollPosition
) {
890 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
891 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
895 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
897 int old_y
= m_yScrollPosition
;
898 m_yScrollPosition
= y_pos
;
900 // Calculate page size i.e. number of scroll units you get on the
901 // current client window
902 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
903 if (noPagePositions
< 1) noPagePositions
= 1;
905 // Correct position if greater than extent of canvas minus
906 // the visible portion of it or if below zero
907 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
908 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
910 if (old_y
!= m_yScrollPosition
) {
911 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
912 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
918 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
920 m_xScrollingEnabled
= x_scroll
;
921 m_yScrollingEnabled
= y_scroll
;
924 // Where the current view starts from
925 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
928 *x
= m_xScrollPosition
;
930 *y
= m_yScrollPosition
;
933 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
936 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
938 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
941 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
944 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
946 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
949 // ----------------------------------------------------------------------------
951 // ----------------------------------------------------------------------------
953 bool wxScrollHelper::ScrollLayout()
955 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
957 // If we're the scroll target, take into account the
958 // virtual size and scrolled position of the window.
961 CalcScrolledPosition(0,0, &x
,&y
);
962 m_win
->GetVirtualSize(&w
, &h
);
963 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
967 // fall back to default for LayoutConstraints
968 return m_win
->wxWindow::Layout();
971 void wxScrollHelper::ScrollDoSetVirtualSize(int x
, int y
)
973 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
976 if (m_win
->GetAutoLayout())
980 // wxWindow's GetBestVirtualSize returns the actual window size,
981 // whereas we want to return the virtual size
982 wxSize
wxScrollHelper::ScrollGetBestVirtualSize() const
984 wxSize
clientSize(m_win
->GetClientSize());
985 if ( m_win
->GetSizer() )
986 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
991 // return the window best size from the given best virtual size
993 wxScrollHelper::ScrollGetWindowSizeForVirtualSize(const wxSize
& size
) const
995 // Only use the content to set the window size in the direction
996 // where there's no scrolling; otherwise we're going to get a huge
997 // window in the direction in which scrolling is enabled
999 GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1001 wxSize minSize
= m_win
->GetMinSize();
1002 if ( !minSize
.IsFullySpecified() )
1003 minSize
= m_win
->GetSize();
1014 // ----------------------------------------------------------------------------
1016 // ----------------------------------------------------------------------------
1018 // Default OnSize resets scrollbars, if any
1019 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
1021 if ( m_targetWindow
->GetAutoLayout() )
1023 wxSize size
= m_targetWindow
->GetBestVirtualSize();
1025 // This will call ::Layout() and ::AdjustScrollbars()
1026 m_win
->SetVirtualSize( size
);
1034 // This calls OnDraw, having adjusted the origin according to the current
1036 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
1038 // don't use m_targetWindow here, this is always called for ourselves
1039 wxPaintDC
dc(m_win
);
1045 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1046 // compatibility here - if we used OnKeyDown(), the programs which process
1047 // arrows themselves in their OnChar() would never get the message and like
1048 // this they always have the priority
1049 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
1051 int stx
, sty
, // view origin
1052 szx
, szy
, // view size (total)
1053 clix
, cliy
; // view size (on screen)
1055 GetViewStart(&stx
, &sty
);
1056 GetTargetSize(&clix
, &cliy
);
1057 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
1059 if( m_xScrollPixelsPerLine
)
1061 clix
/= m_xScrollPixelsPerLine
;
1062 szx
/= m_xScrollPixelsPerLine
;
1069 if( m_yScrollPixelsPerLine
)
1071 cliy
/= m_yScrollPixelsPerLine
;
1072 szy
/= m_yScrollPixelsPerLine
;
1080 int xScrollOld
= m_xScrollPosition
,
1081 yScrollOld
= m_yScrollPosition
;
1084 switch ( event
.GetKeyCode() )
1088 dsty
= sty
- (5 * cliy
/ 6);
1089 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1094 Scroll(-1, sty
+ (5 * cliy
/ 6));
1098 Scroll(0, event
.ControlDown() ? 0 : -1);
1102 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1106 Scroll(-1, sty
- 1);
1110 Scroll(-1, sty
+ 1);
1114 Scroll(stx
- 1, -1);
1118 Scroll(stx
+ 1, -1);
1126 if ( m_xScrollPosition
!= xScrollOld
)
1128 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1130 event
.SetEventObject(m_win
);
1131 m_win
->GetEventHandler()->ProcessEvent(event
);
1134 if ( m_yScrollPosition
!= yScrollOld
)
1136 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1138 event
.SetEventObject(m_win
);
1139 m_win
->GetEventHandler()->ProcessEvent(event
);
1143 // ----------------------------------------------------------------------------
1144 // autoscroll stuff: these functions deal with sending fake scroll events when
1145 // a captured mouse is being held outside the window
1146 // ----------------------------------------------------------------------------
1148 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1150 // only send the event if the window is scrollable in this direction
1151 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1152 return win
->HasScrollbar(event
.GetOrientation());
1155 void wxScrollHelper::StopAutoScrolling()
1158 if ( m_timerAutoScroll
)
1160 delete m_timerAutoScroll
;
1161 m_timerAutoScroll
= (wxTimer
*)NULL
;
1166 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1168 StopAutoScrolling();
1173 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1175 // don't prevent the usual processing of the event from taking place
1178 // when a captured mouse leave a scrolled window we start generate
1179 // scrolling events to allow, for example, extending selection beyond the
1180 // visible area in some controls
1181 if ( wxWindow::GetCapture() == m_targetWindow
)
1183 // where is the mouse leaving?
1185 wxPoint pt
= event
.GetPosition();
1188 orient
= wxHORIZONTAL
;
1191 else if ( pt
.y
< 0 )
1193 orient
= wxVERTICAL
;
1196 else // we're lower or to the right of the window
1198 wxSize size
= m_targetWindow
->GetClientSize();
1199 if ( pt
.x
> size
.x
)
1201 orient
= wxHORIZONTAL
;
1202 pos
= m_xScrollLines
;
1204 else if ( pt
.y
> size
.y
)
1206 orient
= wxVERTICAL
;
1207 pos
= m_yScrollLines
;
1209 else // this should be impossible
1211 // but seems to happen sometimes under wxMSW - maybe it's a bug
1212 // there but for now just ignore it
1214 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1220 // only start the auto scroll timer if the window can be scrolled in
1222 if ( !m_targetWindow
->HasScrollbar(orient
) )
1226 delete m_timerAutoScroll
;
1227 m_timerAutoScroll
= new wxAutoScrollTimer
1229 m_targetWindow
, this,
1230 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1231 : wxEVT_SCROLLWIN_LINEDOWN
,
1235 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1242 #if wxUSE_MOUSEWHEEL
1244 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1246 m_wheelRotation
+= event
.GetWheelRotation();
1247 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1248 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1253 wxScrollWinEvent newEvent
;
1255 newEvent
.SetPosition(0);
1256 newEvent
.SetOrientation(wxVERTICAL
);
1257 newEvent
.SetEventObject(m_win
);
1259 if (event
.IsPageScroll())
1262 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1264 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1266 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1270 lines
*= event
.GetLinesPerAction();
1272 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1274 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1276 int times
= abs(lines
);
1277 for (; times
> 0; times
--)
1278 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1283 #endif // wxUSE_MOUSEWHEEL
1285 // ----------------------------------------------------------------------------
1286 // wxScrolledWindow implementation
1287 // ----------------------------------------------------------------------------
1289 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
1291 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
1292 EVT_PAINT(wxScrolledWindow::OnPaint
)
1295 bool wxScrolledWindow::Create(wxWindow
*parent
,
1300 const wxString
& name
)
1302 m_targetWindow
= this;
1304 MacSetClipChildren( true ) ;
1307 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
1312 wxScrolledWindow::~wxScrolledWindow()
1316 void wxScrolledWindow::OnPaint(wxPaintEvent
& event
)
1318 // the user code didn't really draw the window if we got here, so set this
1319 // flag to try to call OnDraw() later
1320 m_handler
->ResetDrawnFlag();
1326 WXLRESULT
wxScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1330 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1333 // we need to process arrows ourselves for scrolling
1334 if ( nMsg
== WM_GETDLGCODE
)
1336 rc
|= DLGC_WANTARROWS
;