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"
40 #include "wx/settings.h"
44 #include "wx/scrolbar.h"
47 #include "wx/recguard.h"
50 #include <windows.h> // for DLGC_WANTARROWS
51 #include "wx/msw/winundef.h"
55 // For wxRETAINED implementation
56 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
57 //This code switches off the compiler warnings
58 # pragma message disable nosimpint
62 # pragma message enable nosimpint
68 style wxHSCROLL | wxVSCROLL
71 // ----------------------------------------------------------------------------
72 // wxScrollHelperEvtHandler: intercept the events from the window and forward
73 // them to wxScrollHelper
74 // ----------------------------------------------------------------------------
76 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
79 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
81 m_scrollHelper
= scrollHelper
;
84 virtual bool ProcessEvent(wxEvent
& event
);
86 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
89 wxScrollHelper
*m_scrollHelper
;
91 bool m_hasDrawnWindow
;
93 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
97 // ----------------------------------------------------------------------------
98 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
99 // a captured mouse is held outside the window
100 // ----------------------------------------------------------------------------
102 class wxAutoScrollTimer
: public wxTimer
105 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
106 wxEventType eventTypeToSend
,
107 int pos
, int orient
);
109 virtual void Notify();
113 wxScrollHelper
*m_scrollHelper
;
114 wxEventType m_eventType
;
118 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
130 wxScrollHelper
*scroll
,
131 wxEventType eventTypeToSend
,
135 m_scrollHelper
= scroll
;
136 m_eventType
= eventTypeToSend
;
141 void wxAutoScrollTimer::Notify()
143 // only do all this as long as the window is capturing the mouse
144 if ( wxWindow::GetCapture() != m_win
)
148 else // we still capture the mouse, continue generating events
150 // first scroll the window if we are allowed to do it
151 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
152 event1
.SetEventObject(m_win
);
153 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
154 m_win
->GetEventHandler()->ProcessEvent(event1
) )
156 // and then send a pseudo mouse-move event to refresh the selection
157 wxMouseEvent
event2(wxEVT_MOTION
);
158 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
160 // the mouse event coordinates should be client, not screen as
161 // returned by wxGetMousePosition
162 wxWindow
*parentTop
= m_win
;
163 while ( parentTop
->GetParent() )
164 parentTop
= parentTop
->GetParent();
165 wxPoint ptOrig
= parentTop
->GetPosition();
166 event2
.m_x
-= ptOrig
.x
;
167 event2
.m_y
-= ptOrig
.y
;
169 event2
.SetEventObject(m_win
);
171 // FIXME: we don't fill in the other members - ok?
173 m_win
->GetEventHandler()->ProcessEvent(event2
);
175 else // can't scroll further, stop
183 // ----------------------------------------------------------------------------
184 // wxScrollHelperEvtHandler
185 // ----------------------------------------------------------------------------
187 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
189 wxEventType evType
= event
.GetEventType();
191 // the explanation of wxEVT_PAINT processing hack: for historic reasons
192 // there are 2 ways to process this event in classes deriving from
193 // wxScrolledWindow. The user code may
195 // 1. override wxScrolledWindow::OnDraw(dc)
196 // 2. define its own OnPaint() handler
198 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
199 // always processes the draw event, so we can't just try the window
200 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
201 // (the latter would never be called in wxUniversal).
203 // So the solution is to have a flag telling us whether the user code drew
204 // anything in the window. We set it to true here but reset it to false in
205 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
206 // user code defined OnPaint() in the derived class)
207 m_hasDrawnWindow
= true;
209 // pass it on to the real handler
210 bool processed
= wxEvtHandler::ProcessEvent(event
);
212 // always process the size events ourselves, even if the user code handles
213 // them as well, as we need to AdjustScrollbars()
215 // NB: it is important to do it after processing the event in the normal
216 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
217 // scrollbar[s] (dis)appear and it should be seen by the user code
219 if ( evType
== wxEVT_SIZE
)
221 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
228 // normally, nothing more to do here - except if it was a paint event
229 // which wasn't really processed, then we'll try to call our
230 // OnDraw() below (from HandleOnPaint)
231 if ( m_hasDrawnWindow
|| event
.IsCommandEvent() )
237 if ( evType
== wxEVT_PAINT
)
239 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
243 if ( evType
== wxEVT_CHILD_FOCUS
)
245 m_scrollHelper
->HandleOnChildFocus((wxChildFocusEvent
&)event
);
249 // reset the skipped flag (which might have been set to true in
250 // ProcessEvent() above) to be able to test it below
251 bool wasSkipped
= event
.GetSkipped();
255 if ( evType
== wxEVT_SCROLLWIN_TOP
||
256 evType
== wxEVT_SCROLLWIN_BOTTOM
||
257 evType
== wxEVT_SCROLLWIN_LINEUP
||
258 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
259 evType
== wxEVT_SCROLLWIN_PAGEUP
||
260 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
261 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
262 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
264 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
265 if ( !event
.GetSkipped() )
267 // it makes sense to indicate that we processed the message as we
268 // did scroll the window (and also notice that wxAutoScrollTimer
269 // relies on our return value to stop scrolling when we are at top
270 // or bottom already)
276 if ( evType
== wxEVT_ENTER_WINDOW
)
278 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
280 else if ( evType
== wxEVT_LEAVE_WINDOW
)
282 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
285 // Use GTK's own scroll wheel handling in GtkScrolledWindow
287 else if ( evType
== wxEVT_MOUSEWHEEL
)
289 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
293 #endif // wxUSE_MOUSEWHEEL
294 else if ( evType
== wxEVT_CHAR
)
296 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
297 if ( !event
.GetSkipped() )
305 event
.Skip(wasSkipped
);
310 // ----------------------------------------------------------------------------
311 // wxScrollHelper construction
312 // ----------------------------------------------------------------------------
314 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
316 wxASSERT_MSG( win
, _T("associated window can't be NULL in wxScrollHelper") );
318 m_xScrollPixelsPerLine
=
319 m_yScrollPixelsPerLine
=
324 m_xScrollLinesPerPage
=
325 m_yScrollLinesPerPage
= 0;
327 m_xScrollingEnabled
=
328 m_yScrollingEnabled
= true;
337 m_targetWindow
= (wxWindow
*)NULL
;
339 m_timerAutoScroll
= (wxTimer
*)NULL
;
345 m_win
->SetScrollHelper( this );
347 // by default, the associated window is also the target window
348 DoSetTargetWindow(win
);
351 wxScrollHelper::~wxScrollHelper()
358 // ----------------------------------------------------------------------------
359 // setting scrolling parameters
360 // ----------------------------------------------------------------------------
362 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
372 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
375 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
376 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
378 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
379 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
380 (xPos
!= m_xScrollPosition
) ||
381 (yPos
!= m_yScrollPosition
)
384 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
385 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
386 m_xScrollPosition
= xPos
;
387 m_yScrollPosition
= yPos
;
389 int w
= noUnitsX
* pixelsPerUnitX
;
390 int h
= noUnitsY
* pixelsPerUnitY
;
392 // For better backward compatibility we set persisting limits
393 // here not just the size. It makes SetScrollbars 'sticky'
394 // emulating the old non-autoscroll behaviour.
395 // m_targetWindow->SetVirtualSizeHints( w, h );
397 // The above should arguably be deprecated, this however we still need.
399 // take care not to set 0 virtual size, 0 means that we don't have any
400 // scrollbars and hence we should use the real size instead of the virtual
401 // one which is indicated by using wxDefaultCoord
402 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
403 h
? h
: wxDefaultCoord
);
405 if (do_refresh
&& !noRefresh
)
406 m_targetWindow
->Refresh(true, GetScrollRect());
408 #ifndef __WXUNIVERSAL__
409 // If the target is not the same as the window with the scrollbars,
410 // then we need to update the scrollbars here, since they won't have
411 // been updated by SetVirtualSize().
412 if ( m_targetWindow
!= m_win
)
413 #endif // !__WXUNIVERSAL__
417 #ifndef __WXUNIVERSAL__
420 // otherwise this has been done by AdjustScrollbars, above
422 #endif // !__WXUNIVERSAL__
425 // ----------------------------------------------------------------------------
426 // [target] window handling
427 // ----------------------------------------------------------------------------
429 void wxScrollHelper::DeleteEvtHandler()
431 // search for m_handler in the handler list
432 if ( m_win
&& m_handler
)
434 if ( m_win
->RemoveEventHandler(m_handler
) )
438 //else: something is very wrong, so better [maybe] leak memory than
439 // risk a crash because of double deletion
445 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
447 m_targetWindow
= target
;
449 target
->MacSetClipChildren( true ) ;
452 // install the event handler which will intercept the events we're
453 // interested in (but only do it for our real window, not the target window
454 // which we scroll - we don't need to hijack its events)
455 if ( m_targetWindow
== m_win
)
457 // if we already have a handler, delete it first
460 m_handler
= new wxScrollHelperEvtHandler(this);
461 m_targetWindow
->PushEventHandler(m_handler
);
465 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
467 wxCHECK_RET( target
, wxT("target window must not be NULL") );
469 if ( target
== m_targetWindow
)
472 DoSetTargetWindow(target
);
475 wxWindow
*wxScrollHelper::GetTargetWindow() const
477 return m_targetWindow
;
480 // ----------------------------------------------------------------------------
481 // scrolling implementation itself
482 // ----------------------------------------------------------------------------
484 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
486 int nScrollInc
= CalcScrollInc(event
);
487 if ( nScrollInc
== 0 )
489 // can't scroll further
495 bool needsRefresh
= false;
498 int orient
= event
.GetOrientation();
499 if (orient
== wxHORIZONTAL
)
501 if ( m_xScrollingEnabled
)
503 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
512 if ( m_yScrollingEnabled
)
514 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
524 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
525 // otherwise invalidated area could be updated incorrectly later when
526 // ScrollWindow() makes sure they're repainted before scrolling them
528 // wxWindowMac is taking care of making sure the update area is correctly
529 // set up, while not forcing an immediate redraw
531 m_targetWindow
->Update();
535 if (orient
== wxHORIZONTAL
)
537 m_xScrollPosition
+= nScrollInc
;
538 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
542 m_yScrollPosition
+= nScrollInc
;
543 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
548 m_targetWindow
->Refresh(true, GetScrollRect());
552 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
556 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
558 int pos
= event
.GetPosition();
559 int orient
= event
.GetOrientation();
562 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
564 if (orient
== wxHORIZONTAL
)
565 nScrollInc
= - m_xScrollPosition
;
567 nScrollInc
= - m_yScrollPosition
;
569 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
571 if (orient
== wxHORIZONTAL
)
572 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
574 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
576 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
580 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
584 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
586 if (orient
== wxHORIZONTAL
)
587 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
589 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
591 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
593 if (orient
== wxHORIZONTAL
)
594 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
596 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
598 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
599 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
601 if (orient
== wxHORIZONTAL
)
602 nScrollInc
= pos
- m_xScrollPosition
;
604 nScrollInc
= pos
- m_yScrollPosition
;
607 if (orient
== wxHORIZONTAL
)
609 if ( m_xScrollPosition
+ nScrollInc
< 0 )
611 // As -ve as we can go
612 nScrollInc
= -m_xScrollPosition
;
614 else // check for the other bound
616 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
617 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
619 // As +ve as we can go
620 nScrollInc
= posMax
- m_xScrollPosition
;
626 if ( m_yScrollPosition
+ nScrollInc
< 0 )
628 // As -ve as we can go
629 nScrollInc
= -m_yScrollPosition
;
631 else // check for the other bound
633 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
634 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
636 // As +ve as we can go
637 nScrollInc
= posMax
- m_yScrollPosition
;
645 // Adjust the scrollbars - new version.
646 void wxScrollHelper::AdjustScrollbars()
648 static wxRecursionGuardFlag s_flagReentrancy
;
649 wxRecursionGuard
guard(s_flagReentrancy
);
650 if ( guard
.IsInside() )
652 // don't reenter AdjustScrollbars() while another call to
653 // AdjustScrollbars() is in progress because this may lead to calling
654 // ScrollWindow() twice and this can really happen under MSW if
655 // SetScrollbar() call below adds or removes the scrollbar which
656 // changes the window size and hence results in another
657 // AdjustScrollbars() call
664 int oldXScroll
= m_xScrollPosition
;
665 int oldYScroll
= m_yScrollPosition
;
667 // VZ: at least under Windows this loop is useless because when scrollbars
668 // [dis]appear we get a WM_SIZE resulting in another call to
669 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
670 // it here for now but it would be better to ensure that all ports
671 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
672 // necessary, and remove it later
673 // JACS: Stop potential infinite loop by limiting number of iterations
674 int iterationCount
= 0;
675 const int iterationMax
= 5;
680 GetTargetSize(&w
, 0);
682 // scroll lines per page: if 0, no scrolling is needed
685 if ( m_xScrollPixelsPerLine
== 0 )
687 // scrolling is disabled
689 m_xScrollPosition
= 0;
692 else // might need scrolling
694 // Round up integer division to catch any "leftover" client space.
695 const int wVirt
= m_targetWindow
->GetVirtualSize().GetWidth();
696 m_xScrollLines
= (wVirt
+ m_xScrollPixelsPerLine
- 1) / m_xScrollPixelsPerLine
;
698 // Calculate page size i.e. number of scroll units you get on the
699 // current client window.
700 linesPerPage
= w
/ m_xScrollPixelsPerLine
;
702 // Special case. When client and virtual size are very close but
703 // the client is big enough, kill scrollbar.
704 if ((linesPerPage
< m_xScrollLines
) && (w
>= wVirt
)) ++linesPerPage
;
706 if (linesPerPage
>= m_xScrollLines
)
708 // we're big enough to not need scrolling
711 m_xScrollPosition
= 0;
713 else // we do need a scrollbar
715 if ( linesPerPage
< 1 )
718 // Correct position if greater than extent of canvas minus
719 // the visible portion of it or if below zero
720 const int posMax
= m_xScrollLines
- linesPerPage
;
721 if ( m_xScrollPosition
> posMax
)
722 m_xScrollPosition
= posMax
;
723 else if ( m_xScrollPosition
< 0 )
724 m_xScrollPosition
= 0;
728 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
,
729 linesPerPage
, m_xScrollLines
);
731 // The amount by which we scroll when paging
732 SetScrollPageSize(wxHORIZONTAL
, linesPerPage
);
734 GetTargetSize(0, &h
);
736 if ( m_yScrollPixelsPerLine
== 0 )
738 // scrolling is disabled
740 m_yScrollPosition
= 0;
743 else // might need scrolling
745 // Round up integer division to catch any "leftover" client space.
746 const int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
747 m_yScrollLines
= ( hVirt
+ m_yScrollPixelsPerLine
- 1 ) / m_yScrollPixelsPerLine
;
749 // Calculate page size i.e. number of scroll units you get on the
750 // current client window.
751 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
753 // Special case. When client and virtual size are very close but
754 // the client is big enough, kill scrollbar.
755 if ((linesPerPage
< m_yScrollLines
) && (h
>= hVirt
)) ++linesPerPage
;
757 if (linesPerPage
>= m_yScrollLines
)
759 // we're big enough to not need scrolling
762 m_yScrollPosition
= 0;
764 else // we do need a scrollbar
766 if ( linesPerPage
< 1 )
769 // Correct position if greater than extent of canvas minus
770 // the visible portion of it or if below zero
771 const int posMax
= m_yScrollLines
- linesPerPage
;
772 if ( m_yScrollPosition
> posMax
)
773 m_yScrollPosition
= posMax
;
774 else if ( m_yScrollPosition
< 0 )
775 m_yScrollPosition
= 0;
779 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
780 linesPerPage
, m_yScrollLines
);
782 // The amount by which we scroll when paging
783 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
786 // If a scrollbar (dis)appeared as a result of this, adjust them again.
790 GetTargetSize( &w
, &h
);
791 } while ( (w
!= oldw
|| h
!= oldh
) && (iterationCount
< iterationMax
) );
794 // Sorry, some Motif-specific code to implement a backing pixmap
795 // for the wxRETAINED style. Implementing a backing store can't
796 // be entirely generic because it relies on the wxWindowDC implementation
797 // to duplicate X drawing calls for the backing pixmap.
799 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
801 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
803 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
804 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
805 if (m_targetWindow
->GetBackingPixmap() &&
806 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
807 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
809 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
810 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
813 if (!m_targetWindow
->GetBackingPixmap() &&
814 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
816 int depth
= wxDisplayDepth();
817 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
818 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
819 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
820 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
826 if (oldXScroll
!= m_xScrollPosition
)
828 if (m_xScrollingEnabled
)
829 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
832 m_targetWindow
->Refresh(true, GetScrollRect());
835 if (oldYScroll
!= m_yScrollPosition
)
837 if (m_yScrollingEnabled
)
838 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
841 m_targetWindow
->Refresh(true, GetScrollRect());
845 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
847 wxPoint pt
= dc
.GetDeviceOrigin();
849 // It may actually be correct to always query
850 // the m_sign from the DC here, but I leve the
851 // #ifdef GTK for now.
852 if (m_win
->GetLayoutDirection() == wxLayout_RightToLeft
)
853 dc
.SetDeviceOrigin( pt
.x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
,
854 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
857 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
858 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
859 dc
.SetUserScale( m_scaleX
, m_scaleY
);
862 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
864 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
865 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
867 m_xScrollPixelsPerLine
= xstep
;
868 m_yScrollPixelsPerLine
= ystep
;
870 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
871 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
873 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
874 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
875 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
880 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
883 *x_unit
= m_xScrollPixelsPerLine
;
885 *y_unit
= m_yScrollPixelsPerLine
;
889 int wxScrollHelper::GetScrollLines( int orient
) const
891 if ( orient
== wxHORIZONTAL
)
892 return m_xScrollLines
;
894 return m_yScrollLines
;
897 int wxScrollHelper::GetScrollPageSize(int orient
) const
899 if ( orient
== wxHORIZONTAL
)
900 return m_xScrollLinesPerPage
;
902 return m_yScrollLinesPerPage
;
905 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
907 if ( orient
== wxHORIZONTAL
)
908 m_xScrollLinesPerPage
= pageSize
;
910 m_yScrollLinesPerPage
= pageSize
;
914 * Scroll to given position (scroll position, not pixel position)
916 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
921 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
922 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
925 GetTargetSize(&w
, &h
);
927 // compute new position:
928 int new_x
= m_xScrollPosition
;
929 int new_y
= m_yScrollPosition
;
931 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
935 // Calculate page size i.e. number of scroll units you get on the
936 // current client window
937 int noPagePositions
= w
/m_xScrollPixelsPerLine
;
938 if (noPagePositions
< 1) noPagePositions
= 1;
940 // Correct position if greater than extent of canvas minus
941 // the visible portion of it or if below zero
942 new_x
= wxMin( m_xScrollLines
-noPagePositions
, new_x
);
943 new_x
= wxMax( 0, new_x
);
945 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
949 // Calculate page size i.e. number of scroll units you get on the
950 // current client window
951 int noPagePositions
= h
/m_yScrollPixelsPerLine
;
952 if (noPagePositions
< 1) noPagePositions
= 1;
954 // Correct position if greater than extent of canvas minus
955 // the visible portion of it or if below zero
956 new_y
= wxMin( m_yScrollLines
-noPagePositions
, new_y
);
957 new_y
= wxMax( 0, new_y
);
960 if ( new_x
== m_xScrollPosition
&& new_y
== m_yScrollPosition
)
961 return; // nothing to do, the position didn't change
963 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
964 // otherwise invalidated area could be updated incorrectly later when
965 // ScrollWindow() makes sure they're repainted before scrolling them
966 m_targetWindow
->Update();
968 // update the position and scroll the window now:
969 if (m_xScrollPosition
!= new_x
)
971 int old_x
= m_xScrollPosition
;
972 m_xScrollPosition
= new_x
;
973 m_win
->SetScrollPos( wxHORIZONTAL
, new_x
);
974 m_targetWindow
->ScrollWindow( (old_x
-new_x
)*m_xScrollPixelsPerLine
, 0,
978 if (m_yScrollPosition
!= new_y
)
980 int old_y
= m_yScrollPosition
;
981 m_yScrollPosition
= new_y
;
982 m_win
->SetScrollPos( wxVERTICAL
, new_y
);
983 m_targetWindow
->ScrollWindow( 0, (old_y
-new_y
)*m_yScrollPixelsPerLine
,
988 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
990 m_xScrollingEnabled
= x_scroll
;
991 m_yScrollingEnabled
= y_scroll
;
994 // Where the current view starts from
995 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
998 *x
= m_xScrollPosition
;
1000 *y
= m_yScrollPosition
;
1003 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
1006 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
1008 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
1011 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
1014 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
1016 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
1019 // ----------------------------------------------------------------------------
1021 // ----------------------------------------------------------------------------
1023 bool wxScrollHelper::ScrollLayout()
1025 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
1027 // If we're the scroll target, take into account the
1028 // virtual size and scrolled position of the window.
1030 int x
= 0, y
= 0, w
= 0, h
= 0;
1031 CalcScrolledPosition(0,0, &x
,&y
);
1032 m_win
->GetVirtualSize(&w
, &h
);
1033 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
1037 // fall back to default for LayoutConstraints
1038 return m_win
->wxWindow::Layout();
1041 void wxScrollHelper::ScrollDoSetVirtualSize(int x
, int y
)
1043 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
1046 if (m_win
->GetAutoLayout())
1050 // wxWindow's GetBestVirtualSize returns the actual window size,
1051 // whereas we want to return the virtual size
1052 wxSize
wxScrollHelper::ScrollGetBestVirtualSize() const
1054 wxSize
clientSize(m_win
->GetClientSize());
1055 if ( m_win
->GetSizer() )
1056 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
1061 // ----------------------------------------------------------------------------
1063 // ----------------------------------------------------------------------------
1065 // Default OnSize resets scrollbars, if any
1066 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
1068 if ( m_targetWindow
->GetAutoLayout() )
1070 wxSize size
= m_targetWindow
->GetBestVirtualSize();
1072 // This will call ::Layout() and ::AdjustScrollbars()
1073 m_win
->SetVirtualSize( size
);
1081 // This calls OnDraw, having adjusted the origin according to the current
1083 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
1085 // don't use m_targetWindow here, this is always called for ourselves
1086 wxPaintDC
dc(m_win
);
1092 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1093 // compatibility here - if we used OnKeyDown(), the programs which process
1094 // arrows themselves in their OnChar() would never get the message and like
1095 // this they always have the priority
1096 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
1098 int stx
= 0, sty
= 0, // view origin
1099 szx
= 0, szy
= 0, // view size (total)
1100 clix
= 0, cliy
= 0; // view size (on screen)
1102 GetViewStart(&stx
, &sty
);
1103 GetTargetSize(&clix
, &cliy
);
1104 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
1106 if( m_xScrollPixelsPerLine
)
1108 clix
/= m_xScrollPixelsPerLine
;
1109 szx
/= m_xScrollPixelsPerLine
;
1116 if( m_yScrollPixelsPerLine
)
1118 cliy
/= m_yScrollPixelsPerLine
;
1119 szy
/= m_yScrollPixelsPerLine
;
1127 int xScrollOld
= m_xScrollPosition
,
1128 yScrollOld
= m_yScrollPosition
;
1131 switch ( event
.GetKeyCode() )
1134 dsty
= sty
- (5 * cliy
/ 6);
1135 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1139 Scroll(-1, sty
+ (5 * cliy
/ 6));
1143 Scroll(0, event
.ControlDown() ? 0 : -1);
1147 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1151 Scroll(-1, sty
- 1);
1155 Scroll(-1, sty
+ 1);
1159 Scroll(stx
- 1, -1);
1163 Scroll(stx
+ 1, -1);
1171 if ( m_xScrollPosition
!= xScrollOld
)
1173 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1175 evt
.SetEventObject(m_win
);
1176 m_win
->GetEventHandler()->ProcessEvent(evt
);
1179 if ( m_yScrollPosition
!= yScrollOld
)
1181 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1183 evt
.SetEventObject(m_win
);
1184 m_win
->GetEventHandler()->ProcessEvent(evt
);
1188 // ----------------------------------------------------------------------------
1189 // autoscroll stuff: these functions deal with sending fake scroll events when
1190 // a captured mouse is being held outside the window
1191 // ----------------------------------------------------------------------------
1193 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1195 // only send the event if the window is scrollable in this direction
1196 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1197 return win
->HasScrollbar(event
.GetOrientation());
1200 void wxScrollHelper::StopAutoScrolling()
1203 if ( m_timerAutoScroll
)
1205 delete m_timerAutoScroll
;
1206 m_timerAutoScroll
= (wxTimer
*)NULL
;
1211 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1213 StopAutoScrolling();
1218 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1220 // don't prevent the usual processing of the event from taking place
1223 // when a captured mouse leave a scrolled window we start generate
1224 // scrolling events to allow, for example, extending selection beyond the
1225 // visible area in some controls
1226 if ( wxWindow::GetCapture() == m_targetWindow
)
1228 // where is the mouse leaving?
1230 wxPoint pt
= event
.GetPosition();
1233 orient
= wxHORIZONTAL
;
1236 else if ( pt
.y
< 0 )
1238 orient
= wxVERTICAL
;
1241 else // we're lower or to the right of the window
1243 wxSize size
= m_targetWindow
->GetClientSize();
1244 if ( pt
.x
> size
.x
)
1246 orient
= wxHORIZONTAL
;
1247 pos
= m_xScrollLines
;
1249 else if ( pt
.y
> size
.y
)
1251 orient
= wxVERTICAL
;
1252 pos
= m_yScrollLines
;
1254 else // this should be impossible
1256 // but seems to happen sometimes under wxMSW - maybe it's a bug
1257 // there but for now just ignore it
1259 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1265 // only start the auto scroll timer if the window can be scrolled in
1267 if ( !m_targetWindow
->HasScrollbar(orient
) )
1271 delete m_timerAutoScroll
;
1272 m_timerAutoScroll
= new wxAutoScrollTimer
1274 m_targetWindow
, this,
1275 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1276 : wxEVT_SCROLLWIN_LINEDOWN
,
1280 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1287 #if wxUSE_MOUSEWHEEL
1289 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1291 m_wheelRotation
+= event
.GetWheelRotation();
1292 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1293 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1298 wxScrollWinEvent newEvent
;
1300 newEvent
.SetPosition(0);
1301 newEvent
.SetOrientation( event
.GetWheelAxis() == 0 ? wxVERTICAL
: wxHORIZONTAL
);
1302 newEvent
.SetEventObject(m_win
);
1304 if (event
.IsPageScroll())
1307 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1309 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1311 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1315 lines
*= event
.GetLinesPerAction();
1317 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1319 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1321 int times
= abs(lines
);
1322 for (; times
> 0; times
--)
1323 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1328 #endif // wxUSE_MOUSEWHEEL
1330 void wxScrollHelper::HandleOnChildFocus(wxChildFocusEvent
& event
)
1332 // this event should be processed by all windows in parenthood chain,
1333 // e.g. so that nested wxScrolledWindows work correctly
1336 // find the immediate child under which the window receiving focus is:
1337 wxWindow
*win
= event
.GetWindow();
1339 if ( win
== m_targetWindow
)
1340 return; // nothing to do
1342 while ( win
->GetParent() != m_targetWindow
)
1344 win
= win
->GetParent();
1345 wxCHECK_RET( win
, "incorrectly sent wxChildFocusEvent - not our child" );
1348 // if the child is not fully visible, try to scroll it into view:
1350 GetScrollPixelsPerUnit(&stepx
, &stepy
);
1352 // NB: we don't call CalcScrolledPosition() on win->GetPosition() here,
1353 // because children' positions are already scrolled
1354 wxRect
winrect(win
->GetPosition(), win
->GetSize());
1355 wxSize
view(m_targetWindow
->GetClientSize());
1358 GetViewStart(&startx
, &starty
);
1360 // first in vertical direction:
1365 if ( winrect
.GetTop() < 0 )
1367 diff
= winrect
.GetTop();
1369 else if ( winrect
.GetBottom() > view
.y
)
1371 diff
= winrect
.GetBottom() - view
.y
+ 1;
1372 // round up to next scroll step if we can't get exact position,
1373 // so that the window is fully visible:
1377 starty
= (starty
* stepy
+ diff
) / stepy
;
1385 if ( winrect
.GetLeft() < 0 )
1387 diff
= winrect
.GetLeft();
1389 else if ( winrect
.GetRight() > view
.x
)
1391 diff
= winrect
.GetRight() - view
.x
+ 1;
1392 // round up to next scroll step if we can't get exact position,
1393 // so that the window is fully visible:
1397 startx
= (startx
* stepx
+ diff
) / stepx
;
1400 Scroll(startx
, starty
);
1403 // ----------------------------------------------------------------------------
1404 // wxScrolledWindow implementation
1405 // ----------------------------------------------------------------------------
1407 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
1409 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
1410 EVT_PAINT(wxScrolledWindow::OnPaint
)
1413 bool wxScrolledWindow::Create(wxWindow
*parent
,
1418 const wxString
& name
)
1420 m_targetWindow
= this;
1422 MacSetClipChildren( true ) ;
1425 // by default, we're scrollable in both directions (but if one of the
1426 // styles is specified explicitly, we shouldn't add the other one
1428 if ( !(style
& (wxHSCROLL
| wxVSCROLL
)) )
1429 style
|= wxHSCROLL
| wxVSCROLL
;
1431 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1436 wxScrolledWindow::~wxScrolledWindow()
1440 void wxScrolledWindow::OnPaint(wxPaintEvent
& event
)
1442 // the user code didn't really draw the window if we got here, so set this
1443 // flag to try to call OnDraw() later
1444 m_handler
->ResetDrawnFlag();
1449 wxSize
wxScrolledWindow::DoGetBestSize() const
1451 // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1452 // all scrollable windows should behave like this, only those that
1453 // contain children controls within scrollable area
1454 // (i.e., wxScrolledWindow) and other some scrollable windows may
1455 // have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1457 wxSize best
= wxPanel::DoGetBestSize();
1459 if ( GetAutoLayout() )
1461 // Only use the content to set the window size in the direction
1462 // where there's no scrolling; otherwise we're going to get a huge
1463 // window in the direction in which scrolling is enabled
1465 GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1467 // NB: This code used to use *current* size if min size wasn't
1468 // specified, presumably to get some reasonable (i.e., larger than
1469 // minimal) size. But that's a wrong thing to do in GetBestSize(),
1470 // so we use minimal size as specified. If the app needs some
1471 // minimal size for its scrolled window, it should set it and put
1472 // the window into sizer as expandable so that it can use all space
1475 // See also http://svn.wxwidgets.org/viewvc/wx?view=rev&revision=45864
1477 wxSize minSize
= GetMinSize();
1480 best
.x
= minSize
.x
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1483 best
.y
= minSize
.y
+ wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
1490 WXLRESULT
wxScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1494 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1497 // we need to process arrows ourselves for scrolling
1498 if ( nMsg
== WM_GETDLGCODE
)
1500 rc
|= DLGC_WANTARROWS
;