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 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
29 #define XtDisplay XTDISPLAY
32 #include "wx/scrolwin.h"
37 #include "wx/dcclient.h"
45 #include "wx/scrolbar.h"
48 #include "wx/recguard.h"
51 #include <windows.h> // for DLGC_WANTARROWS
52 #include "wx/msw/winundef.h"
56 // For wxRETAINED implementation
57 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
58 //This code switches off the compiler warnings
59 # pragma message disable nosimpint
63 # pragma message enable nosimpint
69 style wxHSCROLL | wxVSCROLL
72 // ----------------------------------------------------------------------------
73 // wxScrollHelperEvtHandler: intercept the events from the window and forward
74 // them to wxScrollHelper
75 // ----------------------------------------------------------------------------
77 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
80 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
82 m_scrollHelper
= scrollHelper
;
85 virtual bool ProcessEvent(wxEvent
& event
);
87 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
90 wxScrollHelper
*m_scrollHelper
;
92 bool m_hasDrawnWindow
;
94 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
98 // ----------------------------------------------------------------------------
99 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
100 // a captured mouse is held outside the window
101 // ----------------------------------------------------------------------------
103 class wxAutoScrollTimer
: public wxTimer
106 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
107 wxEventType eventTypeToSend
,
108 int pos
, int orient
);
110 virtual void Notify();
114 wxScrollHelper
*m_scrollHelper
;
115 wxEventType m_eventType
;
119 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
122 // ============================================================================
124 // ============================================================================
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
131 wxScrollHelper
*scroll
,
132 wxEventType eventTypeToSend
,
136 m_scrollHelper
= scroll
;
137 m_eventType
= eventTypeToSend
;
142 void wxAutoScrollTimer::Notify()
144 // only do all this as long as the window is capturing the mouse
145 if ( wxWindow::GetCapture() != m_win
)
149 else // we still capture the mouse, continue generating events
151 // first scroll the window if we are allowed to do it
152 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
153 event1
.SetEventObject(m_win
);
154 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
155 m_win
->GetEventHandler()->ProcessEvent(event1
) )
157 // and then send a pseudo mouse-move event to refresh the selection
158 wxMouseEvent
event2(wxEVT_MOTION
);
159 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
161 // the mouse event coordinates should be client, not screen as
162 // returned by wxGetMousePosition
163 wxWindow
*parentTop
= m_win
;
164 while ( parentTop
->GetParent() )
165 parentTop
= parentTop
->GetParent();
166 wxPoint ptOrig
= parentTop
->GetPosition();
167 event2
.m_x
-= ptOrig
.x
;
168 event2
.m_y
-= ptOrig
.y
;
170 event2
.SetEventObject(m_win
);
172 // FIXME: we don't fill in the other members - ok?
174 m_win
->GetEventHandler()->ProcessEvent(event2
);
176 else // can't scroll further, stop
184 // ----------------------------------------------------------------------------
185 // wxScrollHelperEvtHandler
186 // ----------------------------------------------------------------------------
188 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
190 wxEventType evType
= event
.GetEventType();
192 // the explanation of wxEVT_PAINT processing hack: for historic reasons
193 // there are 2 ways to process this event in classes deriving from
194 // wxScrolledWindow. The user code may
196 // 1. override wxScrolledWindow::OnDraw(dc)
197 // 2. define its own OnPaint() handler
199 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
200 // always processes the draw event, so we can't just try the window
201 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
202 // (the latter would never be called in wxUniversal).
204 // So the solution is to have a flag telling us whether the user code drew
205 // anything in the window. We set it to true here but reset it to false in
206 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
207 // user code defined OnPaint() in the derived class)
208 m_hasDrawnWindow
= true;
210 // pass it on to the real handler
211 bool processed
= wxEvtHandler::ProcessEvent(event
);
213 // always process the size events ourselves, even if the user code handles
214 // them as well, as we need to AdjustScrollbars()
216 // NB: it is important to do it after processing the event in the normal
217 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
218 // scrollbar[s] (dis)appear and it should be seen by the user code
220 if ( evType
== wxEVT_SIZE
)
222 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
229 // normally, nothing more to do here - except if it was a paint event
230 // which wasn't really processed, then we'll try to call our
231 // OnDraw() below (from HandleOnPaint)
232 if ( m_hasDrawnWindow
|| event
.IsCommandEvent() )
238 if ( evType
== wxEVT_PAINT
)
240 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
244 // reset the skipped flag (which might have been set to true in
245 // ProcessEvent() above) to be able to test it below
246 bool wasSkipped
= event
.GetSkipped();
250 if ( evType
== wxEVT_SCROLLWIN_TOP
||
251 evType
== wxEVT_SCROLLWIN_BOTTOM
||
252 evType
== wxEVT_SCROLLWIN_LINEUP
||
253 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
254 evType
== wxEVT_SCROLLWIN_PAGEUP
||
255 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
256 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
257 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
259 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
260 if ( !event
.GetSkipped() )
262 // it makes sense to indicate that we processed the message as we
263 // did scroll the window (and also notice that wxAutoScrollTimer
264 // relies on our return value to stop scrolling when we are at top
265 // or bottom already)
271 if ( evType
== wxEVT_ENTER_WINDOW
)
273 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
275 else if ( evType
== wxEVT_LEAVE_WINDOW
)
277 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
280 // Use GTK's own scroll wheel handling in GtkScrolledWindow
282 else if ( evType
== wxEVT_MOUSEWHEEL
)
284 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
288 #endif // wxUSE_MOUSEWHEEL
289 else if ( evType
== wxEVT_CHAR
)
291 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
292 if ( !event
.GetSkipped() )
300 event
.Skip(wasSkipped
);
305 // ----------------------------------------------------------------------------
306 // wxScrollHelper construction
307 // ----------------------------------------------------------------------------
309 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
311 wxASSERT_MSG( win
, _T("associated window can't be NULL in wxScrollHelper") );
313 m_xScrollPixelsPerLine
=
314 m_yScrollPixelsPerLine
=
319 m_xScrollLinesPerPage
=
320 m_yScrollLinesPerPage
= 0;
322 m_xScrollingEnabled
=
323 m_yScrollingEnabled
= true;
332 m_targetWindow
= (wxWindow
*)NULL
;
334 m_timerAutoScroll
= (wxTimer
*)NULL
;
340 m_win
->SetScrollHelper( this );
342 // by default, the associated window is also the target window
343 DoSetTargetWindow(win
);
346 wxScrollHelper::~wxScrollHelper()
353 // ----------------------------------------------------------------------------
354 // setting scrolling parameters
355 // ----------------------------------------------------------------------------
357 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
367 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
370 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
371 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
373 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
374 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
375 (xPos
!= m_xScrollPosition
) ||
376 (yPos
!= m_yScrollPosition
)
379 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
380 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
381 m_xScrollPosition
= xPos
;
382 m_yScrollPosition
= yPos
;
384 int w
= noUnitsX
* pixelsPerUnitX
;
385 int h
= noUnitsY
* pixelsPerUnitY
;
387 // For better backward compatibility we set persisting limits
388 // here not just the size. It makes SetScrollbars 'sticky'
389 // emulating the old non-autoscroll behaviour.
390 // m_targetWindow->SetVirtualSizeHints( w, h );
392 // The above should arguably be deprecated, this however we still need.
394 // take care not to set 0 virtual size, 0 means that we don't have any
395 // scrollbars and hence we should use the real size instead of the virtual
396 // one which is indicated by using wxDefaultCoord
397 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
398 h
? h
: wxDefaultCoord
);
400 if (do_refresh
&& !noRefresh
)
401 m_targetWindow
->Refresh(true, GetScrollRect());
403 #ifndef __WXUNIVERSAL__
404 // If the target is not the same as the window with the scrollbars,
405 // then we need to update the scrollbars here, since they won't have
406 // been updated by SetVirtualSize().
407 if ( m_targetWindow
!= m_win
)
408 #endif // !__WXUNIVERSAL__
412 #ifndef __WXUNIVERSAL__
415 // otherwise this has been done by AdjustScrollbars, above
417 #endif // !__WXUNIVERSAL__
420 // ----------------------------------------------------------------------------
421 // [target] window handling
422 // ----------------------------------------------------------------------------
424 void wxScrollHelper::DeleteEvtHandler()
426 // search for m_handler in the handler list
427 if ( m_win
&& m_handler
)
429 if ( m_win
->RemoveEventHandler(m_handler
) )
433 //else: something is very wrong, so better [maybe] leak memory than
434 // risk a crash because of double deletion
440 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
442 m_targetWindow
= target
;
444 target
->MacSetClipChildren( true ) ;
447 // install the event handler which will intercept the events we're
448 // interested in (but only do it for our real window, not the target window
449 // which we scroll - we don't need to hijack its events)
450 if ( m_targetWindow
== m_win
)
452 // if we already have a handler, delete it first
455 m_handler
= new wxScrollHelperEvtHandler(this);
456 m_targetWindow
->PushEventHandler(m_handler
);
460 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
462 wxCHECK_RET( target
, wxT("target window must not be NULL") );
464 if ( target
== m_targetWindow
)
467 DoSetTargetWindow(target
);
470 wxWindow
*wxScrollHelper::GetTargetWindow() const
472 return m_targetWindow
;
475 // ----------------------------------------------------------------------------
476 // scrolling implementation itself
477 // ----------------------------------------------------------------------------
479 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
481 int nScrollInc
= CalcScrollInc(event
);
482 if ( nScrollInc
== 0 )
484 // can't scroll further
490 bool needsRefresh
= false;
493 int orient
= event
.GetOrientation();
494 if (orient
== wxHORIZONTAL
)
496 if ( m_xScrollingEnabled
)
498 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
507 if ( m_yScrollingEnabled
)
509 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
519 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
520 // otherwise invalidated area could be updated incorrectly later when
521 // ScrollWindow() makes sure they're repainted before scrolling them
523 // wxWindowMac is taking care of making sure the update area is correctly
524 // set up, while not forcing an immediate redraw
526 m_targetWindow
->Update();
530 if (orient
== wxHORIZONTAL
)
532 m_xScrollPosition
+= nScrollInc
;
533 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
537 m_yScrollPosition
+= nScrollInc
;
538 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
543 m_targetWindow
->Refresh(true, GetScrollRect());
547 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
551 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
553 int pos
= event
.GetPosition();
554 int orient
= event
.GetOrientation();
557 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
559 if (orient
== wxHORIZONTAL
)
560 nScrollInc
= - m_xScrollPosition
;
562 nScrollInc
= - m_yScrollPosition
;
564 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
566 if (orient
== wxHORIZONTAL
)
567 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
569 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
571 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
575 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
579 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
581 if (orient
== wxHORIZONTAL
)
582 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
584 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
586 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
588 if (orient
== wxHORIZONTAL
)
589 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
591 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
593 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
594 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
596 if (orient
== wxHORIZONTAL
)
597 nScrollInc
= pos
- m_xScrollPosition
;
599 nScrollInc
= pos
- m_yScrollPosition
;
602 if (orient
== wxHORIZONTAL
)
604 if ( m_xScrollPosition
+ nScrollInc
< 0 )
606 // As -ve as we can go
607 nScrollInc
= -m_xScrollPosition
;
609 else // check for the other bound
611 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
612 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
614 // As +ve as we can go
615 nScrollInc
= posMax
- m_xScrollPosition
;
621 if ( m_yScrollPosition
+ nScrollInc
< 0 )
623 // As -ve as we can go
624 nScrollInc
= -m_yScrollPosition
;
626 else // check for the other bound
628 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
629 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
631 // As +ve as we can go
632 nScrollInc
= posMax
- m_yScrollPosition
;
640 // Adjust the scrollbars - new version.
641 void wxScrollHelper::AdjustScrollbars()
643 static wxRecursionGuardFlag s_flagReentrancy
;
644 wxRecursionGuard
guard(s_flagReentrancy
);
645 if ( guard
.IsInside() )
647 // don't reenter AdjustScrollbars() while another call to
648 // AdjustScrollbars() is in progress because this may lead to calling
649 // ScrollWindow() twice and this can really happen under MSW if
650 // SetScrollbar() call below adds or removes the scrollbar which
651 // changes the window size and hence results in another
652 // AdjustScrollbars() call
659 int oldXScroll
= m_xScrollPosition
;
660 int oldYScroll
= m_yScrollPosition
;
662 // VZ: at least under Windows this loop is useless because when scrollbars
663 // [dis]appear we get a WM_SIZE resulting in another call to
664 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
665 // it here for now but it would be better to ensure that all ports
666 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
667 // necessary, and remove it later
668 // JACS: Stop potential infinite loop by limiting number of iterations
669 int iterationCount
= 0;
670 const int iterationMax
= 5;
675 GetTargetSize(&w
, 0);
677 // scroll lines per page: if 0, no scrolling is needed
680 if ( m_xScrollPixelsPerLine
== 0 )
682 // scrolling is disabled
684 m_xScrollPosition
= 0;
687 else // might need scrolling
689 // Round up integer division to catch any "leftover" client space.
690 const int wVirt
= m_targetWindow
->GetVirtualSize().GetWidth();
691 m_xScrollLines
= (wVirt
+ m_xScrollPixelsPerLine
- 1) / m_xScrollPixelsPerLine
;
693 // Calculate page size i.e. number of scroll units you get on the
694 // current client window.
695 linesPerPage
= w
/ m_xScrollPixelsPerLine
;
697 // Special case. When client and virtual size are very close but
698 // the client is big enough, kill scrollbar.
699 if ((linesPerPage
< m_xScrollLines
) && (w
>= wVirt
)) ++linesPerPage
;
701 if (linesPerPage
>= m_xScrollLines
)
703 // we're big enough to not need scrolling
706 m_xScrollPosition
= 0;
708 else // we do need a scrollbar
710 if ( linesPerPage
< 1 )
713 // Correct position if greater than extent of canvas minus
714 // the visible portion of it or if below zero
715 const int posMax
= m_xScrollLines
- linesPerPage
;
716 if ( m_xScrollPosition
> posMax
)
717 m_xScrollPosition
= posMax
;
718 else if ( m_xScrollPosition
< 0 )
719 m_xScrollPosition
= 0;
723 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
,
724 linesPerPage
, m_xScrollLines
);
726 // The amount by which we scroll when paging
727 SetScrollPageSize(wxHORIZONTAL
, linesPerPage
);
729 GetTargetSize(0, &h
);
731 if ( m_yScrollPixelsPerLine
== 0 )
733 // scrolling is disabled
735 m_yScrollPosition
= 0;
738 else // might need scrolling
740 // Round up integer division to catch any "leftover" client space.
741 const int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
742 m_yScrollLines
= ( hVirt
+ m_yScrollPixelsPerLine
- 1 ) / m_yScrollPixelsPerLine
;
744 // Calculate page size i.e. number of scroll units you get on the
745 // current client window.
746 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
748 // Special case. When client and virtual size are very close but
749 // the client is big enough, kill scrollbar.
750 if ((linesPerPage
< m_yScrollLines
) && (h
>= hVirt
)) ++linesPerPage
;
752 if (linesPerPage
>= m_yScrollLines
)
754 // we're big enough to not need scrolling
757 m_yScrollPosition
= 0;
759 else // we do need a scrollbar
761 if ( linesPerPage
< 1 )
764 // Correct position if greater than extent of canvas minus
765 // the visible portion of it or if below zero
766 const int posMax
= m_yScrollLines
- linesPerPage
;
767 if ( m_yScrollPosition
> posMax
)
768 m_yScrollPosition
= posMax
;
769 else if ( m_yScrollPosition
< 0 )
770 m_yScrollPosition
= 0;
774 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
775 linesPerPage
, m_yScrollLines
);
777 // The amount by which we scroll when paging
778 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
781 // If a scrollbar (dis)appeared as a result of this, adjust them again.
785 GetTargetSize( &w
, &h
);
786 } while ( (w
!= oldw
|| h
!= oldh
) && (iterationCount
< iterationMax
) );
789 // Sorry, some Motif-specific code to implement a backing pixmap
790 // for the wxRETAINED style. Implementing a backing store can't
791 // be entirely generic because it relies on the wxWindowDC implementation
792 // to duplicate X drawing calls for the backing pixmap.
794 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
796 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
798 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
799 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
800 if (m_targetWindow
->GetBackingPixmap() &&
801 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
802 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
804 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
805 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
808 if (!m_targetWindow
->GetBackingPixmap() &&
809 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
811 int depth
= wxDisplayDepth();
812 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
813 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
814 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
815 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
821 if (oldXScroll
!= m_xScrollPosition
)
823 if (m_xScrollingEnabled
)
824 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
827 m_targetWindow
->Refresh(true, GetScrollRect());
830 if (oldYScroll
!= m_yScrollPosition
)
832 if (m_yScrollingEnabled
)
833 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
836 m_targetWindow
->Refresh(true, GetScrollRect());
840 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
842 wxPoint pt
= dc
.GetDeviceOrigin();
844 // It may actually be correct to always query
845 // the m_sign from the DC here, but I leve the
846 // #ifdef GTK for now.
847 if (m_win
->GetLayoutDirection() == wxLayout_RightToLeft
)
848 dc
.SetDeviceOrigin( pt
.x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
,
849 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
852 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
853 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
854 dc
.SetUserScale( m_scaleX
, m_scaleY
);
857 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
859 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
860 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
862 m_xScrollPixelsPerLine
= xstep
;
863 m_yScrollPixelsPerLine
= ystep
;
865 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
866 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
868 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
869 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
870 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
875 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
878 *x_unit
= m_xScrollPixelsPerLine
;
880 *y_unit
= m_yScrollPixelsPerLine
;
884 int wxScrollHelper::GetScrollLines( int orient
) const
886 if ( orient
== wxHORIZONTAL
)
887 return m_xScrollLines
;
889 return m_yScrollLines
;
892 int wxScrollHelper::GetScrollPageSize(int orient
) const
894 if ( orient
== wxHORIZONTAL
)
895 return m_xScrollLinesPerPage
;
897 return m_yScrollLinesPerPage
;
900 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
902 if ( orient
== wxHORIZONTAL
)
903 m_xScrollLinesPerPage
= pageSize
;
905 m_yScrollLinesPerPage
= pageSize
;
909 * Scroll to given position (scroll position, not pixel position)
911 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
916 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
917 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
920 GetTargetSize(&w
, &h
);
922 // compute new position:
923 int new_x
= m_xScrollPosition
;
924 int new_y
= m_yScrollPosition
;
926 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
930 // Calculate page size i.e. number of scroll units you get on the
931 // current client window
932 int noPagePositions
= w
/m_xScrollPixelsPerLine
;
933 if (noPagePositions
< 1) noPagePositions
= 1;
935 // Correct position if greater than extent of canvas minus
936 // the visible portion of it or if below zero
937 new_x
= wxMin( m_xScrollLines
-noPagePositions
, new_x
);
938 new_x
= wxMax( 0, new_x
);
940 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
944 // Calculate page size i.e. number of scroll units you get on the
945 // current client window
946 int noPagePositions
= h
/m_yScrollPixelsPerLine
;
947 if (noPagePositions
< 1) noPagePositions
= 1;
949 // Correct position if greater than extent of canvas minus
950 // the visible portion of it or if below zero
951 new_y
= wxMin( m_yScrollLines
-noPagePositions
, new_y
);
952 new_y
= wxMax( 0, new_y
);
955 if ( new_x
== m_xScrollPosition
&& new_y
== m_yScrollPosition
)
956 return; // nothing to do, the position didn't change
958 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
959 // otherwise invalidated area could be updated incorrectly later when
960 // ScrollWindow() makes sure they're repainted before scrolling them
961 m_targetWindow
->Update();
963 // update the position and scroll the window now:
964 if (m_xScrollPosition
!= new_x
)
966 int old_x
= m_xScrollPosition
;
967 m_xScrollPosition
= new_x
;
968 m_win
->SetScrollPos( wxHORIZONTAL
, new_x
);
969 m_targetWindow
->ScrollWindow( (old_x
-new_x
)*m_xScrollPixelsPerLine
, 0,
973 if (m_yScrollPosition
!= new_y
)
975 int old_y
= m_yScrollPosition
;
976 m_yScrollPosition
= new_y
;
977 m_win
->SetScrollPos( wxVERTICAL
, new_y
);
978 m_targetWindow
->ScrollWindow( 0, (old_y
-new_y
)*m_yScrollPixelsPerLine
,
983 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
985 m_xScrollingEnabled
= x_scroll
;
986 m_yScrollingEnabled
= y_scroll
;
989 // Where the current view starts from
990 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
993 *x
= m_xScrollPosition
;
995 *y
= m_yScrollPosition
;
998 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
1001 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
1003 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
1006 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
1009 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
1011 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
1014 // ----------------------------------------------------------------------------
1016 // ----------------------------------------------------------------------------
1018 bool wxScrollHelper::ScrollLayout()
1020 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
1022 // If we're the scroll target, take into account the
1023 // virtual size and scrolled position of the window.
1025 int x
= 0, y
= 0, w
= 0, h
= 0;
1026 CalcScrolledPosition(0,0, &x
,&y
);
1027 m_win
->GetVirtualSize(&w
, &h
);
1028 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
1032 // fall back to default for LayoutConstraints
1033 return m_win
->wxWindow::Layout();
1036 void wxScrollHelper::ScrollDoSetVirtualSize(int x
, int y
)
1038 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
1041 if (m_win
->GetAutoLayout())
1045 // wxWindow's GetBestVirtualSize returns the actual window size,
1046 // whereas we want to return the virtual size
1047 wxSize
wxScrollHelper::ScrollGetBestVirtualSize() const
1049 wxSize
clientSize(m_win
->GetClientSize());
1050 if ( m_win
->GetSizer() )
1051 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
1056 // return the window best size from the given best virtual size
1058 wxScrollHelper::ScrollGetWindowSizeForVirtualSize(const wxSize
& size
) const
1060 // Only use the content to set the window size in the direction
1061 // where there's no scrolling; otherwise we're going to get a huge
1062 // window in the direction in which scrolling is enabled
1064 GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1066 wxSize minSize
= m_win
->GetMinSize();
1067 if ( !minSize
.IsFullySpecified() )
1068 minSize
= m_win
->GetSize();
1079 // ----------------------------------------------------------------------------
1081 // ----------------------------------------------------------------------------
1083 // Default OnSize resets scrollbars, if any
1084 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
1086 if ( m_targetWindow
->GetAutoLayout() )
1088 wxSize size
= m_targetWindow
->GetBestVirtualSize();
1090 // This will call ::Layout() and ::AdjustScrollbars()
1091 m_win
->SetVirtualSize( size
);
1099 // This calls OnDraw, having adjusted the origin according to the current
1101 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
1103 // don't use m_targetWindow here, this is always called for ourselves
1104 wxPaintDC
dc(m_win
);
1110 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1111 // compatibility here - if we used OnKeyDown(), the programs which process
1112 // arrows themselves in their OnChar() would never get the message and like
1113 // this they always have the priority
1114 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
1116 int stx
= 0, sty
= 0, // view origin
1117 szx
= 0, szy
= 0, // view size (total)
1118 clix
= 0, cliy
= 0; // view size (on screen)
1120 GetViewStart(&stx
, &sty
);
1121 GetTargetSize(&clix
, &cliy
);
1122 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
1124 if( m_xScrollPixelsPerLine
)
1126 clix
/= m_xScrollPixelsPerLine
;
1127 szx
/= m_xScrollPixelsPerLine
;
1134 if( m_yScrollPixelsPerLine
)
1136 cliy
/= m_yScrollPixelsPerLine
;
1137 szy
/= m_yScrollPixelsPerLine
;
1145 int xScrollOld
= m_xScrollPosition
,
1146 yScrollOld
= m_yScrollPosition
;
1149 switch ( event
.GetKeyCode() )
1152 dsty
= sty
- (5 * cliy
/ 6);
1153 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1157 Scroll(-1, sty
+ (5 * cliy
/ 6));
1161 Scroll(0, event
.ControlDown() ? 0 : -1);
1165 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1169 Scroll(-1, sty
- 1);
1173 Scroll(-1, sty
+ 1);
1177 Scroll(stx
- 1, -1);
1181 Scroll(stx
+ 1, -1);
1189 if ( m_xScrollPosition
!= xScrollOld
)
1191 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1193 evt
.SetEventObject(m_win
);
1194 m_win
->GetEventHandler()->ProcessEvent(evt
);
1197 if ( m_yScrollPosition
!= yScrollOld
)
1199 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1201 evt
.SetEventObject(m_win
);
1202 m_win
->GetEventHandler()->ProcessEvent(evt
);
1206 // ----------------------------------------------------------------------------
1207 // autoscroll stuff: these functions deal with sending fake scroll events when
1208 // a captured mouse is being held outside the window
1209 // ----------------------------------------------------------------------------
1211 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1213 // only send the event if the window is scrollable in this direction
1214 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1215 return win
->HasScrollbar(event
.GetOrientation());
1218 void wxScrollHelper::StopAutoScrolling()
1221 if ( m_timerAutoScroll
)
1223 delete m_timerAutoScroll
;
1224 m_timerAutoScroll
= (wxTimer
*)NULL
;
1229 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1231 StopAutoScrolling();
1236 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1238 // don't prevent the usual processing of the event from taking place
1241 // when a captured mouse leave a scrolled window we start generate
1242 // scrolling events to allow, for example, extending selection beyond the
1243 // visible area in some controls
1244 if ( wxWindow::GetCapture() == m_targetWindow
)
1246 // where is the mouse leaving?
1248 wxPoint pt
= event
.GetPosition();
1251 orient
= wxHORIZONTAL
;
1254 else if ( pt
.y
< 0 )
1256 orient
= wxVERTICAL
;
1259 else // we're lower or to the right of the window
1261 wxSize size
= m_targetWindow
->GetClientSize();
1262 if ( pt
.x
> size
.x
)
1264 orient
= wxHORIZONTAL
;
1265 pos
= m_xScrollLines
;
1267 else if ( pt
.y
> size
.y
)
1269 orient
= wxVERTICAL
;
1270 pos
= m_yScrollLines
;
1272 else // this should be impossible
1274 // but seems to happen sometimes under wxMSW - maybe it's a bug
1275 // there but for now just ignore it
1277 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1283 // only start the auto scroll timer if the window can be scrolled in
1285 if ( !m_targetWindow
->HasScrollbar(orient
) )
1289 delete m_timerAutoScroll
;
1290 m_timerAutoScroll
= new wxAutoScrollTimer
1292 m_targetWindow
, this,
1293 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1294 : wxEVT_SCROLLWIN_LINEDOWN
,
1298 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1305 #if wxUSE_MOUSEWHEEL
1307 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1309 m_wheelRotation
+= event
.GetWheelRotation();
1310 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1311 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1316 wxScrollWinEvent newEvent
;
1318 newEvent
.SetPosition(0);
1319 newEvent
.SetOrientation( event
.GetWheelAxis() == 0 ? wxVERTICAL
: wxHORIZONTAL
);
1320 newEvent
.SetEventObject(m_win
);
1322 if (event
.IsPageScroll())
1325 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1327 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1329 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1333 lines
*= event
.GetLinesPerAction();
1335 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1337 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1339 int times
= abs(lines
);
1340 for (; times
> 0; times
--)
1341 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1346 #endif // wxUSE_MOUSEWHEEL
1348 // ----------------------------------------------------------------------------
1349 // wxScrolledWindow implementation
1350 // ----------------------------------------------------------------------------
1352 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
1354 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
1355 EVT_PAINT(wxScrolledWindow::OnPaint
)
1358 bool wxScrolledWindow::Create(wxWindow
*parent
,
1363 const wxString
& name
)
1365 m_targetWindow
= this;
1367 MacSetClipChildren( true ) ;
1370 // by default, we're scrollable in both directions (but if one of the
1371 // styles is specified explicitly, we shouldn't add the other one
1373 if ( !(style
& (wxHSCROLL
| wxVSCROLL
)) )
1374 style
|= wxHSCROLL
| wxVSCROLL
;
1376 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1381 wxScrolledWindow::~wxScrolledWindow()
1385 void wxScrolledWindow::OnPaint(wxPaintEvent
& event
)
1387 // the user code didn't really draw the window if we got here, so set this
1388 // flag to try to call OnDraw() later
1389 m_handler
->ResetDrawnFlag();
1395 WXLRESULT
wxScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1399 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1402 // we need to process arrows ourselves for scrolling
1403 if ( nMsg
== WM_GETDLGCODE
)
1405 rc
|= DLGC_WANTARROWS
;