1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/scrolwin.cpp
3 // Purpose: wxGenericScrolledWindow 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
22 #pragma implementation "genscrolwin.h"
26 #define XtDisplay XTDISPLAY
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
36 #if !defined(__WXGTK__) || defined(__WXUNIVERSAL__)
39 #include "wx/dcclient.h"
41 #include "wx/scrolwin.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
66 IMPLEMENT_CLASS(wxScrolledWindow
, wxGenericScrolledWindow
)
70 style wxHSCROLL | wxVSCROLL
73 // ----------------------------------------------------------------------------
74 // wxScrollHelperEvtHandler: intercept the events from the window and forward
75 // them to wxScrollHelper
76 // ----------------------------------------------------------------------------
78 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
81 wxScrollHelperEvtHandler(wxScrollHelper
*scrollHelper
)
83 m_scrollHelper
= scrollHelper
;
86 virtual bool ProcessEvent(wxEvent
& event
);
88 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
91 wxScrollHelper
*m_scrollHelper
;
93 bool m_hasDrawnWindow
;
95 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
)
99 // ----------------------------------------------------------------------------
100 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
101 // a captured mouse is held outside the window
102 // ----------------------------------------------------------------------------
104 class wxAutoScrollTimer
: public wxTimer
107 wxAutoScrollTimer(wxWindow
*winToScroll
, wxScrollHelper
*scroll
,
108 wxEventType eventTypeToSend
,
109 int pos
, int orient
);
111 virtual void Notify();
115 wxScrollHelper
*m_scrollHelper
;
116 wxEventType m_eventType
;
120 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer
)
123 // ============================================================================
125 // ============================================================================
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
132 wxScrollHelper
*scroll
,
133 wxEventType eventTypeToSend
,
137 m_scrollHelper
= scroll
;
138 m_eventType
= eventTypeToSend
;
143 void wxAutoScrollTimer::Notify()
145 // only do all this as long as the window is capturing the mouse
146 if ( wxWindow::GetCapture() != m_win
)
150 else // we still capture the mouse, continue generating events
152 // first scroll the window if we are allowed to do it
153 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
154 event1
.SetEventObject(m_win
);
155 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
156 m_win
->GetEventHandler()->ProcessEvent(event1
) )
158 // and then send a pseudo mouse-move event to refresh the selection
159 wxMouseEvent
event2(wxEVT_MOTION
);
160 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
162 // the mouse event coordinates should be client, not screen as
163 // returned by wxGetMousePosition
164 wxWindow
*parentTop
= m_win
;
165 while ( parentTop
->GetParent() )
166 parentTop
= parentTop
->GetParent();
167 wxPoint ptOrig
= parentTop
->GetPosition();
168 event2
.m_x
-= ptOrig
.x
;
169 event2
.m_y
-= ptOrig
.y
;
171 event2
.SetEventObject(m_win
);
173 // FIXME: we don't fill in the other members - ok?
175 m_win
->GetEventHandler()->ProcessEvent(event2
);
177 else // can't scroll further, stop
185 // ----------------------------------------------------------------------------
186 // wxScrollHelperEvtHandler
187 // ----------------------------------------------------------------------------
189 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
191 wxEventType evType
= event
.GetEventType();
193 // the explanation of wxEVT_PAINT processing hack: for historic reasons
194 // there are 2 ways to process this event in classes deriving from
195 // wxScrolledWindow. The user code may
197 // 1. override wxScrolledWindow::OnDraw(dc)
198 // 2. define its own OnPaint() handler
200 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
201 // always processes the draw event, so we can't just try the window
202 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
203 // (the latter would never be called in wxUniversal).
205 // So the solution is to have a flag telling us whether the user code drew
206 // anything in the window. We set it to true here but reset it to false in
207 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
208 // user code defined OnPaint() in the derived class)
209 m_hasDrawnWindow
= true;
211 // pass it on to the real handler
212 bool processed
= wxEvtHandler::ProcessEvent(event
);
214 // always process the size events ourselves, even if the user code handles
215 // them as well, as we need to AdjustScrollbars()
217 // NB: it is important to do it after processing the event in the normal
218 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
219 // scrollbar[s] (dis)appear and it should be seen by the user code
221 if ( evType
== wxEVT_SIZE
)
223 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
230 // normally, nothing more to do here - except if it was a paint event
231 // which wasn't really processed, then we'll try to call our
232 // OnDraw() below (from HandleOnPaint)
233 if ( m_hasDrawnWindow
)
239 // reset the skipped flag to false as it might have been set to true in
240 // ProcessEvent() above
243 if ( evType
== wxEVT_PAINT
)
245 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
249 if ( evType
== wxEVT_SCROLLWIN_TOP
||
250 evType
== wxEVT_SCROLLWIN_BOTTOM
||
251 evType
== wxEVT_SCROLLWIN_LINEUP
||
252 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
253 evType
== wxEVT_SCROLLWIN_PAGEUP
||
254 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
255 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
256 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
258 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
259 return !event
.GetSkipped();
262 if ( evType
== wxEVT_ENTER_WINDOW
)
264 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
266 else if ( evType
== wxEVT_LEAVE_WINDOW
)
268 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
271 else if ( evType
== wxEVT_MOUSEWHEEL
)
273 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
275 #endif // wxUSE_MOUSEWHEEL
276 else if ( evType
== wxEVT_CHAR
)
278 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
279 return !event
.GetSkipped();
285 // ----------------------------------------------------------------------------
286 // wxScrollHelper construction
287 // ----------------------------------------------------------------------------
289 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
291 m_xScrollPixelsPerLine
=
292 m_yScrollPixelsPerLine
=
297 m_xScrollLinesPerPage
=
298 m_yScrollLinesPerPage
= 0;
300 m_xScrollingEnabled
=
301 m_yScrollingEnabled
= true;
310 m_targetWindow
= (wxWindow
*)NULL
;
312 m_timerAutoScroll
= (wxTimer
*)NULL
;
320 wxScrollHelper::~wxScrollHelper()
327 // ----------------------------------------------------------------------------
328 // setting scrolling parameters
329 // ----------------------------------------------------------------------------
331 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
341 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
344 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
345 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
347 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
348 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
349 (xPos
!= m_xScrollPosition
) ||
350 (yPos
!= m_yScrollPosition
)
353 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
354 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
355 m_xScrollPosition
= xPos
;
356 m_yScrollPosition
= yPos
;
358 int w
= noUnitsX
* pixelsPerUnitX
;
359 int h
= noUnitsY
* pixelsPerUnitY
;
361 // For better backward compatibility we set persisting limits
362 // here not just the size. It makes SetScrollbars 'sticky'
363 // emulating the old non-autoscroll behaviour.
364 // m_targetWindow->SetVirtualSizeHints( w, h );
366 // The above should arguably be deprecated, this however we still need.
368 // take care not to set 0 virtual size, 0 means that we don't have any
369 // scrollbars and hence we should use the real size instead of the virtual
370 // one which is indicated by using wxDefaultCoord
371 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
372 h
? h
: wxDefaultCoord
);
374 if (do_refresh
&& !noRefresh
)
375 m_targetWindow
->Refresh(true, GetScrollRect());
377 #ifndef __WXUNIVERSAL__
378 // If the target is not the same as the window with the scrollbars,
379 // then we need to update the scrollbars here, since they won't have
380 // been updated by SetVirtualSize().
381 if ( m_targetWindow
!= m_win
)
382 #endif // !__WXUNIVERSAL__
386 #ifndef __WXUNIVERSAL__
389 // otherwise this has been done by AdjustScrollbars, above
391 #endif // !__WXUNIVERSAL__
394 // ----------------------------------------------------------------------------
395 // [target] window handling
396 // ----------------------------------------------------------------------------
398 void wxScrollHelper::DeleteEvtHandler()
400 // search for m_handler in the handler list
401 if ( m_win
&& m_handler
)
403 if ( m_win
->RemoveEventHandler(m_handler
) )
407 //else: something is very wrong, so better [maybe] leak memory than
408 // risk a crash because of double deletion
414 void wxScrollHelper::SetWindow(wxWindow
*win
)
416 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
420 // by default, the associated window is also the target window
421 DoSetTargetWindow(win
);
424 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
426 m_targetWindow
= target
;
428 target
->MacSetClipChildren( true ) ;
431 // install the event handler which will intercept the events we're
432 // interested in (but only do it for our real window, not the target window
433 // which we scroll - we don't need to hijack its events)
434 if ( m_targetWindow
== m_win
)
436 // if we already have a handler, delete it first
439 m_handler
= new wxScrollHelperEvtHandler(this);
440 m_targetWindow
->PushEventHandler(m_handler
);
444 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
446 wxCHECK_RET( target
, wxT("target window must not be NULL") );
448 if ( target
== m_targetWindow
)
451 DoSetTargetWindow(target
);
454 wxWindow
*wxScrollHelper::GetTargetWindow() const
456 return m_targetWindow
;
459 // ----------------------------------------------------------------------------
460 // scrolling implementation itself
461 // ----------------------------------------------------------------------------
463 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
465 int nScrollInc
= CalcScrollInc(event
);
466 if ( nScrollInc
== 0 )
468 // can't scroll further
474 int orient
= event
.GetOrientation();
475 if (orient
== wxHORIZONTAL
)
477 m_xScrollPosition
+= nScrollInc
;
478 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
482 m_yScrollPosition
+= nScrollInc
;
483 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
486 bool needsRefresh
= false;
489 if (orient
== wxHORIZONTAL
)
491 if ( m_xScrollingEnabled
)
493 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
502 if ( m_yScrollingEnabled
)
504 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
514 m_targetWindow
->Refresh(true, GetScrollRect());
518 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
522 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
524 int pos
= event
.GetPosition();
525 int orient
= event
.GetOrientation();
528 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
530 if (orient
== wxHORIZONTAL
)
531 nScrollInc
= - m_xScrollPosition
;
533 nScrollInc
= - m_yScrollPosition
;
535 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
537 if (orient
== wxHORIZONTAL
)
538 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
540 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
542 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
546 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
550 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
552 if (orient
== wxHORIZONTAL
)
553 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
555 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
557 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
559 if (orient
== wxHORIZONTAL
)
560 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
562 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
564 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
565 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
567 if (orient
== wxHORIZONTAL
)
568 nScrollInc
= pos
- m_xScrollPosition
;
570 nScrollInc
= pos
- m_yScrollPosition
;
573 if (orient
== wxHORIZONTAL
)
575 if (m_xScrollPixelsPerLine
> 0)
577 if ( m_xScrollPosition
+ nScrollInc
< 0 )
579 // As -ve as we can go
580 nScrollInc
= -m_xScrollPosition
;
582 else // check for the other bound
584 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
585 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
587 // As +ve as we can go
588 nScrollInc
= posMax
- m_xScrollPosition
;
593 m_targetWindow
->Refresh(true, GetScrollRect());
597 if ( m_yScrollPixelsPerLine
> 0 )
599 if ( m_yScrollPosition
+ nScrollInc
< 0 )
601 // As -ve as we can go
602 nScrollInc
= -m_yScrollPosition
;
604 else // check for the other bound
606 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
607 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
609 // As +ve as we can go
610 nScrollInc
= posMax
- m_yScrollPosition
;
616 // VZ: why do we do this? (FIXME)
617 m_targetWindow
->Refresh(true, GetScrollRect());
624 // Adjust the scrollbars - new version.
625 void wxScrollHelper::AdjustScrollbars()
627 static wxRecursionGuardFlag s_flagReentrancy
;
628 wxRecursionGuard
guard(s_flagReentrancy
);
629 if ( guard
.IsInside() )
631 // don't reenter AdjustScrollbars() while another call to
632 // AdjustScrollbars() is in progress because this may lead to calling
633 // ScrollWindow() twice and this can really happen under MSW if
634 // SetScrollbar() call below adds or removes the scrollbar which
635 // changes the window size and hence results in another
636 // AdjustScrollbars() call
643 int oldXScroll
= m_xScrollPosition
;
644 int oldYScroll
= m_yScrollPosition
;
646 // VZ: at least under Windows this loop is useless because when scrollbars
647 // [dis]appear we get a WM_SIZE resulting in another call to
648 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
649 // it here for now but it would be better to ensure that all ports
650 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
651 // necessary, and remove it later
654 GetTargetSize(&w
, 0);
656 // scroll lines per page: if 0, no scrolling is needed
659 if ( m_xScrollPixelsPerLine
== 0 )
661 // scrolling is disabled
663 m_xScrollPosition
= 0;
666 else // might need scrolling
668 // Round up integer division to catch any "leftover" client space.
669 const int wVirt
= m_targetWindow
->GetVirtualSize().GetWidth();
670 m_xScrollLines
= (wVirt
+ m_xScrollPixelsPerLine
- 1) / m_xScrollPixelsPerLine
;
672 // Calculate page size i.e. number of scroll units you get on the
673 // current client window.
674 linesPerPage
= w
/ m_xScrollPixelsPerLine
;
676 // Special case. When client and virtual size are very close but
677 // the client is big enough, kill scrollbar.
678 if ((linesPerPage
< m_xScrollLines
) && (w
>= wVirt
)) ++linesPerPage
;
680 if (linesPerPage
>= m_xScrollLines
)
682 // we're big enough to not need scrolling
685 m_xScrollPosition
= 0;
687 else // we do need a scrollbar
689 if ( linesPerPage
< 1 )
692 // Correct position if greater than extent of canvas minus
693 // the visible portion of it or if below zero
694 const int posMax
= m_xScrollLines
- linesPerPage
;
695 if ( m_xScrollPosition
> posMax
)
696 m_xScrollPosition
= posMax
;
697 else if ( m_xScrollPosition
< 0 )
698 m_xScrollPosition
= 0;
702 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
,
703 linesPerPage
, m_xScrollLines
);
705 // The amount by which we scroll when paging
706 SetScrollPageSize(wxHORIZONTAL
, linesPerPage
);
708 GetTargetSize(0, &h
);
710 if ( m_yScrollPixelsPerLine
== 0 )
712 // scrolling is disabled
714 m_yScrollPosition
= 0;
717 else // might need scrolling
719 // Round up integer division to catch any "leftover" client space.
720 const int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
721 m_yScrollLines
= ( hVirt
+ m_yScrollPixelsPerLine
- 1 ) / m_yScrollPixelsPerLine
;
723 // Calculate page size i.e. number of scroll units you get on the
724 // current client window.
725 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
727 // Special case. When client and virtual size are very close but
728 // the client is big enough, kill scrollbar.
729 if ((linesPerPage
< m_yScrollLines
) && (h
>= hVirt
)) ++linesPerPage
;
731 if (linesPerPage
>= m_yScrollLines
)
733 // we're big enough to not need scrolling
736 m_yScrollPosition
= 0;
738 else // we do need a scrollbar
740 if ( linesPerPage
< 1 )
743 // Correct position if greater than extent of canvas minus
744 // the visible portion of it or if below zero
745 const int posMax
= m_yScrollLines
- linesPerPage
;
746 if ( m_yScrollPosition
> posMax
)
747 m_yScrollPosition
= posMax
;
748 else if ( m_yScrollPosition
< 0 )
749 m_yScrollPosition
= 0;
753 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
754 linesPerPage
, m_yScrollLines
);
756 // The amount by which we scroll when paging
757 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
760 // If a scrollbar (dis)appeared as a result of this, adjust them again.
764 GetTargetSize( &w
, &h
);
765 } while ( w
!= oldw
|| h
!= oldh
);
768 // Sorry, some Motif-specific code to implement a backing pixmap
769 // for the wxRETAINED style. Implementing a backing store can't
770 // be entirely generic because it relies on the wxWindowDC implementation
771 // to duplicate X drawing calls for the backing pixmap.
773 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
775 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
777 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
778 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
779 if (m_targetWindow
->GetBackingPixmap() &&
780 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
781 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
783 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
784 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
787 if (!m_targetWindow
->GetBackingPixmap() &&
788 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
790 int depth
= wxDisplayDepth();
791 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
792 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
793 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
794 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
800 if (oldXScroll
!= m_xScrollPosition
)
802 if (m_xScrollingEnabled
)
803 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
806 m_targetWindow
->Refresh(true, GetScrollRect());
809 if (oldYScroll
!= m_yScrollPosition
)
811 if (m_yScrollingEnabled
)
812 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
815 m_targetWindow
->Refresh(true, GetScrollRect());
819 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
821 wxPoint pt
= dc
.GetDeviceOrigin();
822 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
823 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
824 dc
.SetUserScale( m_scaleX
, m_scaleY
);
827 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
829 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
830 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
832 m_xScrollPixelsPerLine
= xstep
;
833 m_yScrollPixelsPerLine
= ystep
;
835 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
836 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
838 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
839 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
840 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
845 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
848 *x_unit
= m_xScrollPixelsPerLine
;
850 *y_unit
= m_yScrollPixelsPerLine
;
853 int wxScrollHelper::GetScrollPageSize(int orient
) const
855 if ( orient
== wxHORIZONTAL
)
856 return m_xScrollLinesPerPage
;
858 return m_yScrollLinesPerPage
;
861 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
863 if ( orient
== wxHORIZONTAL
)
864 m_xScrollLinesPerPage
= pageSize
;
866 m_yScrollLinesPerPage
= pageSize
;
870 * Scroll to given position (scroll position, not pixel position)
872 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
877 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
878 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
881 GetTargetSize(&w
, &h
);
883 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
885 int old_x
= m_xScrollPosition
;
886 m_xScrollPosition
= x_pos
;
888 // Calculate page size i.e. number of scroll units you get on the
889 // current client window
890 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
891 if (noPagePositions
< 1) noPagePositions
= 1;
893 // Correct position if greater than extent of canvas minus
894 // the visible portion of it or if below zero
895 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
896 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
898 if (old_x
!= m_xScrollPosition
) {
899 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
900 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
904 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
906 int old_y
= m_yScrollPosition
;
907 m_yScrollPosition
= y_pos
;
909 // Calculate page size i.e. number of scroll units you get on the
910 // current client window
911 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
912 if (noPagePositions
< 1) noPagePositions
= 1;
914 // Correct position if greater than extent of canvas minus
915 // the visible portion of it or if below zero
916 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
917 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
919 if (old_y
!= m_yScrollPosition
) {
920 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
921 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
927 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
929 m_xScrollingEnabled
= x_scroll
;
930 m_yScrollingEnabled
= y_scroll
;
933 // Where the current view starts from
934 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
937 *x
= m_xScrollPosition
;
939 *y
= m_yScrollPosition
;
942 #if WXWIN_COMPATIBILITY_2_2
944 void wxScrollHelper::ViewStart(int *x
, int *y
) const
946 GetViewStart( x
, y
);
949 #endif // WXWIN_COMPATIBILITY_2_2
951 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
954 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
956 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
959 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
962 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
964 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
967 // ----------------------------------------------------------------------------
969 // ----------------------------------------------------------------------------
971 // Default OnSize resets scrollbars, if any
972 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
974 if ( m_targetWindow
->GetAutoLayout() )
976 wxSize size
= m_targetWindow
->GetBestVirtualSize();
978 // This will call ::Layout() and ::AdjustScrollbars()
979 m_win
->SetVirtualSize( size
);
987 // This calls OnDraw, having adjusted the origin according to the current
989 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
991 // don't use m_targetWindow here, this is always called for ourselves
998 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
999 // compatibility here - if we used OnKeyDown(), the programs which process
1000 // arrows themselves in their OnChar() would never get the message and like
1001 // this they always have the priority
1002 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
1004 int stx
, sty
, // view origin
1005 szx
, szy
, // view size (total)
1006 clix
, cliy
; // view size (on screen)
1008 GetViewStart(&stx
, &sty
);
1009 GetTargetSize(&clix
, &cliy
);
1010 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
1012 if( m_xScrollPixelsPerLine
)
1014 clix
/= m_xScrollPixelsPerLine
;
1015 szx
/= m_xScrollPixelsPerLine
;
1022 if( m_yScrollPixelsPerLine
)
1024 cliy
/= m_yScrollPixelsPerLine
;
1025 szy
/= m_yScrollPixelsPerLine
;
1033 int xScrollOld
= m_xScrollPosition
,
1034 yScrollOld
= m_yScrollPosition
;
1037 switch ( event
.GetKeyCode() )
1041 dsty
= sty
- (5 * cliy
/ 6);
1042 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1047 Scroll(-1, sty
+ (5 * cliy
/ 6));
1051 Scroll(0, event
.ControlDown() ? 0 : -1);
1055 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1059 Scroll(-1, sty
- 1);
1063 Scroll(-1, sty
+ 1);
1067 Scroll(stx
- 1, -1);
1071 Scroll(stx
+ 1, -1);
1079 if ( m_xScrollPosition
!= xScrollOld
)
1081 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1083 event
.SetEventObject(m_win
);
1084 m_win
->GetEventHandler()->ProcessEvent(event
);
1087 if ( m_yScrollPosition
!= yScrollOld
)
1089 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1091 event
.SetEventObject(m_win
);
1092 m_win
->GetEventHandler()->ProcessEvent(event
);
1096 // ----------------------------------------------------------------------------
1097 // autoscroll stuff: these functions deal with sending fake scroll events when
1098 // a captured mouse is being held outside the window
1099 // ----------------------------------------------------------------------------
1101 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1103 // only send the event if the window is scrollable in this direction
1104 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1105 return win
->HasScrollbar(event
.GetOrientation());
1108 void wxScrollHelper::StopAutoScrolling()
1111 if ( m_timerAutoScroll
)
1113 delete m_timerAutoScroll
;
1114 m_timerAutoScroll
= (wxTimer
*)NULL
;
1119 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1121 StopAutoScrolling();
1126 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1128 // don't prevent the usual processing of the event from taking place
1131 // when a captured mouse leave a scrolled window we start generate
1132 // scrolling events to allow, for example, extending selection beyond the
1133 // visible area in some controls
1134 if ( wxWindow::GetCapture() == m_targetWindow
)
1136 // where is the mouse leaving?
1138 wxPoint pt
= event
.GetPosition();
1141 orient
= wxHORIZONTAL
;
1144 else if ( pt
.y
< 0 )
1146 orient
= wxVERTICAL
;
1149 else // we're lower or to the right of the window
1151 wxSize size
= m_targetWindow
->GetClientSize();
1152 if ( pt
.x
> size
.x
)
1154 orient
= wxHORIZONTAL
;
1155 pos
= m_xScrollLines
;
1157 else if ( pt
.y
> size
.y
)
1159 orient
= wxVERTICAL
;
1160 pos
= m_yScrollLines
;
1162 else // this should be impossible
1164 // but seems to happen sometimes under wxMSW - maybe it's a bug
1165 // there but for now just ignore it
1167 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1173 // only start the auto scroll timer if the window can be scrolled in
1175 if ( !m_targetWindow
->HasScrollbar(orient
) )
1179 delete m_timerAutoScroll
;
1180 m_timerAutoScroll
= new wxAutoScrollTimer
1182 m_targetWindow
, this,
1183 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1184 : wxEVT_SCROLLWIN_LINEDOWN
,
1188 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1195 #if wxUSE_MOUSEWHEEL
1197 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1199 m_wheelRotation
+= event
.GetWheelRotation();
1200 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1201 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1206 wxScrollWinEvent newEvent
;
1208 newEvent
.SetPosition(0);
1209 newEvent
.SetOrientation(wxVERTICAL
);
1210 newEvent
.SetEventObject(m_win
);
1212 if (event
.IsPageScroll())
1215 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1217 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1219 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1223 lines
*= event
.GetLinesPerAction();
1225 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1227 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1229 int times
= abs(lines
);
1230 for (; times
> 0; times
--)
1231 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1236 #endif // wxUSE_MOUSEWHEEL
1238 // ----------------------------------------------------------------------------
1239 // wxGenericScrolledWindow implementation
1240 // ----------------------------------------------------------------------------
1242 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1244 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1245 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1248 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1253 const wxString
& name
)
1255 m_targetWindow
= this;
1257 MacSetClipChildren( true ) ;
1260 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
1265 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1269 bool wxGenericScrolledWindow::Layout()
1271 if (GetSizer() && m_targetWindow
== this)
1273 // If we're the scroll target, take into account the
1274 // virtual size and scrolled position of the window.
1277 CalcScrolledPosition(0,0, &x
,&y
);
1278 GetVirtualSize(&w
, &h
);
1279 GetSizer()->SetDimension(x
, y
, w
, h
);
1283 // fall back to default for LayoutConstraints
1284 return wxPanel::Layout();
1287 void wxGenericScrolledWindow::DoSetVirtualSize(int x
, int y
)
1289 wxPanel::DoSetVirtualSize( x
, y
);
1292 if (GetAutoLayout())
1296 // wxWindow's GetBestVirtualSize returns the actual window size,
1297 // whereas we want to return the virtual size
1298 wxSize
wxGenericScrolledWindow::GetBestVirtualSize() const
1300 wxSize
clientSize( GetClientSize() );
1303 wxSize
minSize( GetSizer()->CalcMin() );
1305 return wxSize( wxMax( clientSize
.x
, minSize
.x
), wxMax( clientSize
.y
, minSize
.y
) );
1311 // return the size best suited for the current window
1312 // (this isn't a virtual size, this is a sensible size for the window)
1313 wxSize
wxGenericScrolledWindow::DoGetBestSize() const
1319 wxSize b
= GetSizer()->GetMinSize();
1321 // Only use the content to set the window size in the direction
1322 // where there's no scrolling; otherwise we're going to get a huge
1323 // window in the direction in which scrolling is enabled
1325 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1328 if ( GetMinSize().IsFullySpecified() )
1329 minSize
= GetMinSize();
1331 minSize
= GetSize();
1340 return wxWindow::DoGetBestSize();
1342 // Add any difference between size and client size
1343 wxSize diff
= GetSize() - GetClientSize();
1344 best
.x
+= wxMax(0, diff
.x
);
1345 best
.y
+= wxMax(0, diff
.y
);
1350 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1352 // the user code didn't really draw the window if we got here, so set this
1353 // flag to try to call OnDraw() later
1354 m_handler
->ResetDrawnFlag();
1361 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1365 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1368 // we need to process arrows ourselves for scrolling
1369 if ( nMsg
== WM_GETDLGCODE
)
1371 rc
|= DLGC_WANTARROWS
;