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) wxWindows 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"
49 #include <windows.h> // for DLGC_WANTARROWS
53 // For wxRETAINED implementation
54 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
55 //This code switches off the compiler warnings
56 # pragma message disable nosimpint
60 # pragma message enable nosimpint
64 IMPLEMENT_CLASS(wxScrolledWindow
, wxGenericScrolledWindow
)
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
)
237 // reset the skipped flag to FALSE as it might have been set to TRUE in
238 // ProcessEvent() above
241 if ( evType
== wxEVT_PAINT
)
243 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
247 if ( evType
== wxEVT_SCROLLWIN_TOP
||
248 evType
== wxEVT_SCROLLWIN_BOTTOM
||
249 evType
== wxEVT_SCROLLWIN_LINEUP
||
250 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
251 evType
== wxEVT_SCROLLWIN_PAGEUP
||
252 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
253 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
254 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
256 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
257 return !event
.GetSkipped();
260 if ( evType
== wxEVT_ENTER_WINDOW
)
262 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
264 else if ( evType
== wxEVT_LEAVE_WINDOW
)
266 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
269 else if ( evType
== wxEVT_MOUSEWHEEL
)
271 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
273 #endif // wxUSE_MOUSEWHEEL
274 else if ( evType
== wxEVT_CHAR
)
276 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
277 return !event
.GetSkipped();
283 // ----------------------------------------------------------------------------
284 // wxScrollHelper construction
285 // ----------------------------------------------------------------------------
287 wxScrollHelper::wxScrollHelper(wxWindow
*win
)
289 m_xScrollPixelsPerLine
=
290 m_yScrollPixelsPerLine
=
295 m_xScrollLinesPerPage
=
296 m_yScrollLinesPerPage
= 0;
298 m_xScrollingEnabled
=
299 m_yScrollingEnabled
= TRUE
;
308 m_targetWindow
= (wxWindow
*)NULL
;
310 m_timerAutoScroll
= (wxTimer
*)NULL
;
318 wxScrollHelper::~wxScrollHelper()
325 // ----------------------------------------------------------------------------
326 // setting scrolling parameters
327 // ----------------------------------------------------------------------------
329 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
,
339 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
342 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
343 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
345 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
346 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
347 (xPos
!= m_xScrollPosition
) ||
348 (yPos
!= m_yScrollPosition
)
351 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
352 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
353 m_xScrollPosition
= xPos
;
354 m_yScrollPosition
= yPos
;
356 int w
= noUnitsX
* pixelsPerUnitX
;
357 int h
= noUnitsY
* pixelsPerUnitY
;
359 // For better backward compatibility we set persisting limits
360 // here not just the size. It makes SetScrollbars 'sticky'
361 // emulating the old non-autoscroll behaviour.
363 m_targetWindow
->SetVirtualSizeHints( w
, h
);
365 // The above should arguably be deprecated, this however we still need.
367 m_targetWindow
->SetVirtualSize( w
, h
);
369 if (do_refresh
&& !noRefresh
)
370 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
372 #ifndef __WXUNIVERSAL__
373 // If the target is not the same as the window with the scrollbars,
374 // then we need to update the scrollbars here, since they won't have
375 // been updated by SetVirtualSize().
376 if ( m_targetWindow
!= m_win
)
377 #endif // !__WXUNIVERSAL__
381 #ifndef __WXUNIVERSAL__
384 // otherwise this has been done by AdjustScrollbars, above
386 m_targetWindow
->Update() ;
389 #endif // !__WXUNIVERSAL__
392 // ----------------------------------------------------------------------------
393 // [target] window handling
394 // ----------------------------------------------------------------------------
396 void wxScrollHelper::DeleteEvtHandler()
398 // search for m_handler in the handler list
399 if ( m_win
&& m_handler
)
401 if ( m_win
->RemoveEventHandler(m_handler
) )
405 //else: something is very wrong, so better [maybe] leak memory than
406 // risk a crash because of double deletion
412 void wxScrollHelper::SetWindow(wxWindow
*win
)
414 wxCHECK_RET( win
, _T("wxScrollHelper needs a window to scroll") );
418 // by default, the associated window is also the target window
419 DoSetTargetWindow(win
);
422 void wxScrollHelper::DoSetTargetWindow(wxWindow
*target
)
424 m_targetWindow
= target
;
426 // install the event handler which will intercept the events we're
427 // interested in (but only do it for our real window, not the target window
428 // which we scroll - we don't need to hijack its events)
429 if ( m_targetWindow
== m_win
)
431 // if we already have a handler, delete it first
434 m_handler
= new wxScrollHelperEvtHandler(this);
435 m_targetWindow
->PushEventHandler(m_handler
);
439 void wxScrollHelper::SetTargetWindow(wxWindow
*target
)
441 wxCHECK_RET( target
, wxT("target window must not be NULL") );
443 if ( target
== m_targetWindow
)
446 DoSetTargetWindow(target
);
449 wxWindow
*wxScrollHelper::GetTargetWindow() const
451 return m_targetWindow
;
454 // ----------------------------------------------------------------------------
455 // scrolling implementation itself
456 // ----------------------------------------------------------------------------
458 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent
& event
)
460 int nScrollInc
= CalcScrollInc(event
);
461 if ( nScrollInc
== 0 )
463 // can't scroll further
469 int orient
= event
.GetOrientation();
470 if (orient
== wxHORIZONTAL
)
472 m_xScrollPosition
+= nScrollInc
;
473 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
477 m_yScrollPosition
+= nScrollInc
;
478 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
481 bool needsRefresh
= FALSE
;
484 if (orient
== wxHORIZONTAL
)
486 if ( m_xScrollingEnabled
)
488 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
497 if ( m_yScrollingEnabled
)
499 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
509 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
513 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
517 m_targetWindow
->Update() ;
521 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent
& event
)
523 int pos
= event
.GetPosition();
524 int orient
= event
.GetOrientation();
527 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
529 if (orient
== wxHORIZONTAL
)
530 nScrollInc
= - m_xScrollPosition
;
532 nScrollInc
= - m_yScrollPosition
;
534 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
536 if (orient
== wxHORIZONTAL
)
537 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
539 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
541 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
545 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
549 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
551 if (orient
== wxHORIZONTAL
)
552 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
554 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
556 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
558 if (orient
== wxHORIZONTAL
)
559 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
561 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
563 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
564 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
566 if (orient
== wxHORIZONTAL
)
567 nScrollInc
= pos
- m_xScrollPosition
;
569 nScrollInc
= pos
- m_yScrollPosition
;
572 if (orient
== wxHORIZONTAL
)
574 if (m_xScrollPixelsPerLine
> 0)
577 GetTargetSize(&w
, &h
);
579 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
580 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
584 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
585 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
586 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
587 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
590 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
594 if ( m_yScrollPixelsPerLine
> 0 )
596 if ( m_yScrollPosition
+ nScrollInc
< 0 )
598 // As -ve as we can go
599 nScrollInc
= -m_yScrollPosition
;
601 else // check for the other bound
603 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
604 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
606 // As +ve as we can go
607 nScrollInc
= posMax
- m_yScrollPosition
;
613 // VZ: why do we do this? (FIXME)
614 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
621 // Adjust the scrollbars - new version.
622 void wxScrollHelper::AdjustScrollbars()
625 m_targetWindow
->Update();
631 int oldXScroll
= m_xScrollPosition
;
632 int oldYScroll
= m_yScrollPosition
;
634 // VZ: at least under Windows this loop is useless because when scrollbars
635 // [dis]appear we get a WM_SIZE resulting in another call to
636 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
637 // it here for now but it would be better to ensure that all ports
638 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
639 // necessary, and remove it later
642 GetTargetSize(&w
, 0);
644 if (m_xScrollPixelsPerLine
== 0)
647 m_xScrollPosition
= 0;
648 m_win
->SetScrollbar (wxHORIZONTAL
, 0, 0, 0, FALSE
);
652 m_xScrollLines
= m_targetWindow
->GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine
;
654 // Calculate page size i.e. number of scroll units you get on the
655 // current client window
656 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
657 if (noPagePositions
< 1) noPagePositions
= 1;
658 if ( noPagePositions
> m_xScrollLines
)
659 noPagePositions
= m_xScrollLines
;
661 // Correct position if greater than extent of canvas minus
662 // the visible portion of it or if below zero
663 m_xScrollPosition
= wxMin( m_xScrollLines
- noPagePositions
, m_xScrollPosition
);
664 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
666 m_win
->SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, m_xScrollLines
);
667 // The amount by which we scroll when paging
668 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
671 GetTargetSize(0, &h
);
673 // scroll lines per page: if 0, no scrolling is needed
676 if ( m_yScrollPixelsPerLine
== 0 )
678 // scrolling is disabled
680 m_yScrollPosition
= 0;
683 else // might need scrolling
685 int hVirt
= m_targetWindow
->GetVirtualSize().GetHeight();
686 m_yScrollLines
= hVirt
/ m_yScrollPixelsPerLine
;
688 // Calculate page size i.e. number of scroll units you get on the
689 // current client window
690 linesPerPage
= h
/ m_yScrollPixelsPerLine
;
691 if ( linesPerPage
>= m_yScrollLines
)
693 // we're big enough to not need scrolling
696 m_yScrollPosition
= 0;
698 else // we do need a scrollbar
700 if ( linesPerPage
< 1 )
703 // Correct position if greater than extent of canvas minus
704 // the visible portion of it or if below zero
705 const int posMax
= m_yScrollLines
- linesPerPage
;
706 if ( m_yScrollPosition
> posMax
)
707 m_yScrollPosition
= posMax
;
708 else if ( m_yScrollPosition
< 0 )
709 m_yScrollPosition
= 0;
713 m_win
->SetScrollbar(wxVERTICAL
, m_yScrollPosition
,
714 linesPerPage
, m_yScrollLines
);
716 // The amount by which we scroll when paging
717 SetScrollPageSize(wxVERTICAL
, linesPerPage
);
720 // If a scrollbar (dis)appeared as a result of this, adjust them again.
724 GetTargetSize( &w
, &h
);
725 } while ( w
!= oldw
&& h
!= oldh
);
728 // Sorry, some Motif-specific code to implement a backing pixmap
729 // for the wxRETAINED style. Implementing a backing store can't
730 // be entirely generic because it relies on the wxWindowDC implementation
731 // to duplicate X drawing calls for the backing pixmap.
733 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
735 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
737 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
738 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
739 if (m_targetWindow
->GetBackingPixmap() &&
740 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
741 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
743 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
744 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
747 if (!m_targetWindow
->GetBackingPixmap() &&
748 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
750 int depth
= wxDisplayDepth();
751 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
752 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
753 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
754 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
760 if (oldXScroll
!= m_xScrollPosition
)
762 if (m_xScrollingEnabled
)
763 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
766 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
769 if (oldYScroll
!= m_yScrollPosition
)
771 if (m_yScrollingEnabled
)
772 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
775 m_targetWindow
->Refresh(TRUE
, GetScrollRect());
779 m_targetWindow
->Update();
783 void wxScrollHelper::DoPrepareDC(wxDC
& dc
)
785 wxPoint pt
= dc
.GetDeviceOrigin();
786 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
787 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
788 dc
.SetUserScale( m_scaleX
, m_scaleY
);
791 void wxScrollHelper::SetScrollRate( int xstep
, int ystep
)
793 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
794 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
796 m_xScrollPixelsPerLine
= xstep
;
797 m_yScrollPixelsPerLine
= ystep
;
799 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
800 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
802 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
803 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
804 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
809 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
812 *x_unit
= m_xScrollPixelsPerLine
;
814 *y_unit
= m_yScrollPixelsPerLine
;
817 int wxScrollHelper::GetScrollPageSize(int orient
) const
819 if ( orient
== wxHORIZONTAL
)
820 return m_xScrollLinesPerPage
;
822 return m_yScrollLinesPerPage
;
825 void wxScrollHelper::SetScrollPageSize(int orient
, int pageSize
)
827 if ( orient
== wxHORIZONTAL
)
828 m_xScrollLinesPerPage
= pageSize
;
830 m_yScrollLinesPerPage
= pageSize
;
834 * Scroll to given position (scroll position, not pixel position)
836 void wxScrollHelper::Scroll( int x_pos
, int y_pos
)
841 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
842 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
845 m_targetWindow
->Update();
849 GetTargetSize(&w
, &h
);
851 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
853 int old_x
= m_xScrollPosition
;
854 m_xScrollPosition
= x_pos
;
856 // Calculate page size i.e. number of scroll units you get on the
857 // current client window
858 int noPagePositions
= (int) ( (w
/(double)m_xScrollPixelsPerLine
) + 0.5 );
859 if (noPagePositions
< 1) noPagePositions
= 1;
861 // Correct position if greater than extent of canvas minus
862 // the visible portion of it or if below zero
863 m_xScrollPosition
= wxMin( m_xScrollLines
-noPagePositions
, m_xScrollPosition
);
864 m_xScrollPosition
= wxMax( 0, m_xScrollPosition
);
866 if (old_x
!= m_xScrollPosition
) {
867 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
868 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0,
872 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
874 int old_y
= m_yScrollPosition
;
875 m_yScrollPosition
= y_pos
;
877 // Calculate page size i.e. number of scroll units you get on the
878 // current client window
879 int noPagePositions
= (int) ( (h
/(double)m_yScrollPixelsPerLine
) + 0.5 );
880 if (noPagePositions
< 1) noPagePositions
= 1;
882 // Correct position if greater than extent of canvas minus
883 // the visible portion of it or if below zero
884 m_yScrollPosition
= wxMin( m_yScrollLines
-noPagePositions
, m_yScrollPosition
);
885 m_yScrollPosition
= wxMax( 0, m_yScrollPosition
);
887 if (old_y
!= m_yScrollPosition
) {
888 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
889 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
,
895 m_targetWindow
->Update();
900 void wxScrollHelper::EnableScrolling (bool x_scroll
, bool y_scroll
)
902 m_xScrollingEnabled
= x_scroll
;
903 m_yScrollingEnabled
= y_scroll
;
906 // Where the current view starts from
907 void wxScrollHelper::GetViewStart (int *x
, int *y
) const
910 *x
= m_xScrollPosition
;
912 *y
= m_yScrollPosition
;
915 void wxScrollHelper::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
918 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
920 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
923 void wxScrollHelper::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
926 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
928 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
931 // ----------------------------------------------------------------------------
933 // ----------------------------------------------------------------------------
935 // Default OnSize resets scrollbars, if any
936 void wxScrollHelper::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
938 if( m_win
->GetAutoLayout() || m_targetWindow
->GetAutoLayout() )
940 if ( m_targetWindow
!= m_win
)
941 m_targetWindow
->FitInside();
945 // FIXME: Something is really weird here... This should be
946 // called by FitInside above (and apparently is), yet the
947 // scrollsub sample will get the scrollbar wrong if resized
948 // quickly. This masks the bug, but is surely not the right
956 // This calls OnDraw, having adjusted the origin according to the current
958 void wxScrollHelper::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
960 // don't use m_targetWindow here, this is always called for ourselves
967 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
968 // compatibility here - if we used OnKeyDown(), the programs which process
969 // arrows themselves in their OnChar() would never get the message and like
970 // this they always have the priority
971 void wxScrollHelper::HandleOnChar(wxKeyEvent
& event
)
973 int stx
, sty
, // view origin
974 szx
, szy
, // view size (total)
975 clix
, cliy
; // view size (on screen)
977 GetViewStart(&stx
, &sty
);
978 GetTargetSize(&clix
, &cliy
);
979 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
981 if( m_xScrollPixelsPerLine
)
983 clix
/= m_xScrollPixelsPerLine
;
984 szx
/= m_xScrollPixelsPerLine
;
991 if( m_yScrollPixelsPerLine
)
993 cliy
/= m_yScrollPixelsPerLine
;
994 szy
/= m_yScrollPixelsPerLine
;
1002 int xScrollOld
= m_xScrollPosition
,
1003 yScrollOld
= m_yScrollPosition
;
1006 switch ( event
.GetKeyCode() )
1010 dsty
= sty
- (5 * cliy
/ 6);
1011 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
1016 Scroll(-1, sty
+ (5 * cliy
/ 6));
1020 Scroll(0, event
.ControlDown() ? 0 : -1);
1024 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1028 Scroll(-1, sty
- 1);
1032 Scroll(-1, sty
+ 1);
1036 Scroll(stx
- 1, -1);
1040 Scroll(stx
+ 1, -1);
1048 if ( m_xScrollPosition
!= xScrollOld
)
1050 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
1052 event
.SetEventObject(m_win
);
1053 m_win
->GetEventHandler()->ProcessEvent(event
);
1056 if ( m_yScrollPosition
!= yScrollOld
)
1058 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
1060 event
.SetEventObject(m_win
);
1061 m_win
->GetEventHandler()->ProcessEvent(event
);
1065 // ----------------------------------------------------------------------------
1066 // autoscroll stuff: these functions deal with sending fake scroll events when
1067 // a captured mouse is being held outside the window
1068 // ----------------------------------------------------------------------------
1070 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
1072 // only send the event if the window is scrollable in this direction
1073 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
1074 return win
->HasScrollbar(event
.GetOrientation());
1077 void wxScrollHelper::StopAutoScrolling()
1080 if ( m_timerAutoScroll
)
1082 delete m_timerAutoScroll
;
1083 m_timerAutoScroll
= (wxTimer
*)NULL
;
1088 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent
& event
)
1090 StopAutoScrolling();
1095 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent
& event
)
1097 // don't prevent the usual processing of the event from taking place
1100 // when a captured mouse leave a scrolled window we start generate
1101 // scrolling events to allow, for example, extending selection beyond the
1102 // visible area in some controls
1103 if ( wxWindow::GetCapture() == m_targetWindow
)
1105 // where is the mouse leaving?
1107 wxPoint pt
= event
.GetPosition();
1110 orient
= wxHORIZONTAL
;
1113 else if ( pt
.y
< 0 )
1115 orient
= wxVERTICAL
;
1118 else // we're lower or to the right of the window
1120 wxSize size
= m_targetWindow
->GetClientSize();
1121 if ( pt
.x
> size
.x
)
1123 orient
= wxHORIZONTAL
;
1124 pos
= m_xScrollLines
;
1126 else if ( pt
.y
> size
.y
)
1128 orient
= wxVERTICAL
;
1129 pos
= m_yScrollLines
;
1131 else // this should be impossible
1133 // but seems to happen sometimes under wxMSW - maybe it's a bug
1134 // there but for now just ignore it
1136 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1142 // only start the auto scroll timer if the window can be scrolled in
1144 if ( !m_targetWindow
->HasScrollbar(orient
) )
1148 delete m_timerAutoScroll
;
1149 m_timerAutoScroll
= new wxAutoScrollTimer
1151 m_targetWindow
, this,
1152 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1153 : wxEVT_SCROLLWIN_LINEDOWN
,
1157 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1162 #if wxUSE_MOUSEWHEEL
1164 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent
& event
)
1166 m_wheelRotation
+= event
.GetWheelRotation();
1167 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1168 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1173 wxScrollWinEvent newEvent
;
1175 newEvent
.SetPosition(0);
1176 newEvent
.SetOrientation(wxVERTICAL
);
1177 newEvent
.m_eventObject
= m_win
;
1179 if (event
.IsPageScroll())
1182 newEvent
.m_eventType
= wxEVT_SCROLLWIN_PAGEUP
;
1184 newEvent
.m_eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
1186 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1190 lines
*= event
.GetLinesPerAction();
1192 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEUP
;
1194 newEvent
.m_eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
1196 int times
= abs(lines
);
1197 for (; times
> 0; times
--)
1198 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1203 #endif // wxUSE_MOUSEWHEEL
1205 // ----------------------------------------------------------------------------
1206 // wxGenericScrolledWindow implementation
1207 // ----------------------------------------------------------------------------
1209 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow
, wxPanel
)
1211 BEGIN_EVENT_TABLE(wxGenericScrolledWindow
, wxPanel
)
1212 EVT_PAINT(wxGenericScrolledWindow::OnPaint
)
1215 bool wxGenericScrolledWindow::Create(wxWindow
*parent
,
1220 const wxString
& name
)
1222 m_targetWindow
= this;
1224 bool ok
= wxPanel::Create(parent
, id
, pos
, size
, style
, name
);
1229 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1233 bool wxGenericScrolledWindow::Layout()
1235 if (GetSizer() && m_targetWindow
== this)
1237 // If we're the scroll target, take into account the
1238 // virtual size and scrolled position of the window.
1241 CalcScrolledPosition(0,0, &x
,&y
);
1242 GetVirtualSize(&w
, &h
);
1243 GetSizer()->SetDimension(x
, y
, w
, h
);
1247 // fall back to default for LayoutConstraints
1248 return wxPanel::Layout();
1251 void wxGenericScrolledWindow::DoSetVirtualSize(int x
, int y
)
1253 wxPanel::DoSetVirtualSize( x
, y
);
1256 #if wxUSE_CONSTRAINTS
1257 if (GetAutoLayout())
1262 void wxGenericScrolledWindow::OnPaint(wxPaintEvent
& event
)
1264 // the user code didn't really draw the window if we got here, so set this
1265 // flag to try to call OnDraw() later
1266 m_handler
->ResetDrawnFlag();
1273 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg
,
1277 long rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
1280 // we need to process arrows ourselves for scrolling
1281 if ( nMsg
== WM_GETDLGCODE
)
1283 rc
|= DLGC_WANTARROWS
;
1292 #if WXWIN_COMPATIBILITY
1294 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page
, int *y_page
) const
1296 *x_page
= GetScrollPageSize(wxHORIZONTAL
);
1297 *y_page
= GetScrollPageSize(wxVERTICAL
);
1300 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x
, int y
, float *xx
, float *yy
) const
1303 *xx
= (float)(x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
);
1305 *yy
= (float)(y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
);
1308 #endif // WXWIN_COMPATIBILITY