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"
28 #include "wx/scrolwin.h"
33 #include "wx/dcclient.h"
36 #include "wx/settings.h"
40 #include "wx/scrolbar.h"
43 #include "wx/recguard.h"
46 #include <windows.h> // for DLGC_WANTARROWS
47 #include "wx/msw/winundef.h"
51 // For wxRETAINED implementation
52 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
53 //This code switches off the compiler warnings
54 # pragma message disable nosimpint
58 # pragma message enable nosimpint
64 style wxHSCROLL | wxVSCROLL
67 // ----------------------------------------------------------------------------
68 // wxScrollHelperEvtHandler: intercept the events from the window and forward
69 // them to wxScrollHelper
70 // ----------------------------------------------------------------------------
72 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
75 wxScrollHelperEvtHandler(wxScrollHelperBase
*scrollHelper
)
77 m_scrollHelper
= scrollHelper
;
80 virtual bool ProcessEvent(wxEvent
& event
);
83 wxScrollHelperBase
*m_scrollHelper
;
85 wxDECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
);
89 // ----------------------------------------------------------------------------
90 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
91 // a captured mouse is held outside the window
92 // ----------------------------------------------------------------------------
94 class wxAutoScrollTimer
: public wxTimer
97 wxAutoScrollTimer(wxWindow
*winToScroll
,
98 wxScrollHelperBase
*scroll
,
99 wxEventType eventTypeToSend
,
100 int pos
, int orient
);
102 virtual void Notify();
106 wxScrollHelperBase
*m_scrollHelper
;
107 wxEventType m_eventType
;
111 wxDECLARE_NO_COPY_CLASS(wxAutoScrollTimer
);
114 // ============================================================================
116 // ============================================================================
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
123 wxScrollHelperBase
*scroll
,
124 wxEventType eventTypeToSend
,
128 m_scrollHelper
= scroll
;
129 m_eventType
= eventTypeToSend
;
134 void wxAutoScrollTimer::Notify()
136 // only do all this as long as the window is capturing the mouse
137 if ( wxWindow::GetCapture() != m_win
)
141 else // we still capture the mouse, continue generating events
143 // first scroll the window if we are allowed to do it
144 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
145 event1
.SetEventObject(m_win
);
146 event1
.SetId(m_win
->GetId());
147 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
148 m_win
->GetEventHandler()->ProcessEvent(event1
) )
150 // and then send a pseudo mouse-move event to refresh the selection
151 wxMouseEvent
event2(wxEVT_MOTION
);
152 event2
.SetPosition(wxGetMousePosition());
154 // the mouse event coordinates should be client, not screen as
155 // returned by wxGetMousePosition
156 wxWindow
*parentTop
= m_win
;
157 while ( parentTop
->GetParent() )
158 parentTop
= parentTop
->GetParent();
159 wxPoint ptOrig
= parentTop
->GetPosition();
160 event2
.m_x
-= ptOrig
.x
;
161 event2
.m_y
-= ptOrig
.y
;
163 event2
.SetEventObject(m_win
);
165 wxMouseState mouseState
= wxGetMouseState();
167 event2
.m_leftDown
= mouseState
.LeftDown();
168 event2
.m_middleDown
= mouseState
.MiddleDown();
169 event2
.m_rightDown
= mouseState
.RightDown();
171 event2
.m_shiftDown
= mouseState
.ShiftDown();
172 event2
.m_controlDown
= mouseState
.ControlDown();
173 event2
.m_altDown
= mouseState
.AltDown();
174 event2
.m_metaDown
= mouseState
.MetaDown();
176 m_win
->GetEventHandler()->ProcessEvent(event2
);
178 else // can't scroll further, stop
186 // ----------------------------------------------------------------------------
187 // wxScrollHelperEvtHandler
188 // ----------------------------------------------------------------------------
190 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
192 wxEventType evType
= event
.GetEventType();
194 // Pass it on to the real handler: notice that we must not call
195 // ProcessEvent() on this object itself as it wouldn't pass it to the next
196 // handler (i.e. the real window) if we're called from a previous handler
197 // (as indicated by "process here only" flag being set) and we do want to
198 // execute the handler defined in the window we're associated with right
199 // now, without waiting until TryAfter() is called from wxEvtHandler.
200 bool processed
= m_nextHandler
->ProcessEvent(event
);
202 // always process the size events ourselves, even if the user code handles
203 // them as well, as we need to AdjustScrollbars()
205 // NB: it is important to do it after processing the event in the normal
206 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
207 // scrollbar[s] (dis)appear and it should be seen by the user code
209 if ( evType
== wxEVT_SIZE
)
211 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
215 if ( processed
&& event
.IsCommandEvent())
218 // For wxEVT_PAINT the user code can either handle this event as usual or
219 // override virtual OnDraw(), so if the event hasn't been handled we need
220 // to call this virtual function ourselves.
222 #ifndef __WXUNIVERSAL__
223 // in wxUniversal "processed" will always be true, because
224 // all windows use the paint event to draw themselves.
225 // In this case we can't use this flag to determine if a custom
226 // paint event handler already drew our window and we just
227 // call OnDraw() anyway.
229 #endif // !__WXUNIVERSAL__
230 evType
== wxEVT_PAINT
)
232 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
236 if ( evType
== wxEVT_CHILD_FOCUS
)
238 m_scrollHelper
->HandleOnChildFocus((wxChildFocusEvent
&)event
);
242 // reset the skipped flag (which might have been set to true in
243 // ProcessEvent() above) to be able to test it below
244 bool wasSkipped
= event
.GetSkipped();
248 if ( evType
== wxEVT_SCROLLWIN_TOP
||
249 evType
== wxEVT_SCROLLWIN_BOTTOM
||
250 evType
== wxEVT_SCROLLWIN_LINEUP
||
251 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
252 evType
== wxEVT_SCROLLWIN_PAGEUP
||
253 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
254 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
255 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
257 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
258 if ( !event
.GetSkipped() )
260 // it makes sense to indicate that we processed the message as we
261 // did scroll the window (and also notice that wxAutoScrollTimer
262 // relies on our return value to stop scrolling when we are at top
263 // or bottom already)
269 if ( evType
== wxEVT_ENTER_WINDOW
)
271 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
273 else if ( evType
== wxEVT_LEAVE_WINDOW
)
275 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
278 // Use GTK's own scroll wheel handling in GtkScrolledWindow
280 else if ( evType
== wxEVT_MOUSEWHEEL
)
282 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
286 #endif // wxUSE_MOUSEWHEEL
287 else if ( evType
== wxEVT_CHAR
)
289 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
290 if ( !event
.GetSkipped() )
297 event
.Skip(wasSkipped
);
299 // We called ProcessEvent() on the next handler, meaning that we explicitly
300 // worked around the request to process the event in this handler only. As
301 // explained above, this is unfortunately really necessary but the trouble
302 // is that the event will continue to be post-processed by the previous
303 // handler resulting in duplicate calls to event handlers. Call the special
304 // function below to prevent this from happening, base class DoTryChain()
305 // will check for it and behave accordingly.
307 // And if we're not called from DoTryChain(), this won't do anything anyhow.
308 event
.DidntHonourProcessOnlyIn();
313 // ============================================================================
314 // wxScrollHelperBase implementation
315 // ============================================================================
317 // ----------------------------------------------------------------------------
318 // wxScrollHelperBase construction
319 // ----------------------------------------------------------------------------
321 wxScrollHelperBase::wxScrollHelperBase(wxWindow
*win
)
323 wxASSERT_MSG( win
, wxT("associated window can't be NULL in wxScrollHelper") );
325 m_xScrollPixelsPerLine
=
326 m_yScrollPixelsPerLine
=
331 m_xScrollLinesPerPage
=
332 m_yScrollLinesPerPage
= 0;
334 m_xScrollingEnabled
=
335 m_yScrollingEnabled
= true;
337 m_kbdScrollingEnabled
= true;
346 m_targetWindow
= NULL
;
348 m_timerAutoScroll
= NULL
;
354 m_win
->SetScrollHelper(static_cast<wxScrollHelper
*>(this));
356 // by default, the associated window is also the target window
357 DoSetTargetWindow(win
);
360 wxScrollHelperBase::~wxScrollHelperBase()
367 // ----------------------------------------------------------------------------
368 // setting scrolling parameters
369 // ----------------------------------------------------------------------------
371 void wxScrollHelperBase::SetScrollbars(int pixelsPerUnitX
,
379 // Convert positions expressed in scroll units to positions in pixels.
380 int xPosInPixels
= (xPos
+ m_xScrollPosition
)*m_xScrollPixelsPerLine
,
381 yPosInPixels
= (yPos
+ m_yScrollPosition
)*m_yScrollPixelsPerLine
;
385 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
386 (noUnitsX
< m_xScrollLines
&& xPosInPixels
> pixelsPerUnitX
* noUnitsX
) ||
388 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
389 (noUnitsY
< m_yScrollLines
&& yPosInPixels
> pixelsPerUnitY
* noUnitsY
) ||
390 (xPos
!= m_xScrollPosition
) ||
391 (yPos
!= m_yScrollPosition
)
394 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
395 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
396 m_xScrollPosition
= xPos
;
397 m_yScrollPosition
= yPos
;
399 int w
= noUnitsX
* pixelsPerUnitX
;
400 int h
= noUnitsY
* pixelsPerUnitY
;
402 // For better backward compatibility we set persisting limits
403 // here not just the size. It makes SetScrollbars 'sticky'
404 // emulating the old non-autoscroll behaviour.
405 // m_targetWindow->SetVirtualSizeHints( w, h );
407 // The above should arguably be deprecated, this however we still need.
409 // take care not to set 0 virtual size, 0 means that we don't have any
410 // scrollbars and hence we should use the real size instead of the virtual
411 // one which is indicated by using wxDefaultCoord
412 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
413 h
? h
: wxDefaultCoord
);
415 if (do_refresh
&& !noRefresh
)
416 m_targetWindow
->Refresh(true, GetScrollRect());
418 #ifndef __WXUNIVERSAL__
419 // If the target is not the same as the window with the scrollbars,
420 // then we need to update the scrollbars here, since they won't have
421 // been updated by SetVirtualSize().
422 if ( m_targetWindow
!= m_win
)
423 #endif // !__WXUNIVERSAL__
427 #ifndef __WXUNIVERSAL__
430 // otherwise this has been done by AdjustScrollbars, above
432 #endif // !__WXUNIVERSAL__
435 // ----------------------------------------------------------------------------
436 // [target] window handling
437 // ----------------------------------------------------------------------------
439 void wxScrollHelperBase::DeleteEvtHandler()
441 // search for m_handler in the handler list
442 if ( m_win
&& m_handler
)
444 if ( m_win
->RemoveEventHandler(m_handler
) )
448 //else: something is very wrong, so better [maybe] leak memory than
449 // risk a crash because of double deletion
455 void wxScrollHelperBase::DoSetTargetWindow(wxWindow
*target
)
457 m_targetWindow
= target
;
459 target
->MacSetClipChildren( true ) ;
462 // install the event handler which will intercept the events we're
463 // interested in (but only do it for our real window, not the target window
464 // which we scroll - we don't need to hijack its events)
465 if ( m_targetWindow
== m_win
)
467 // if we already have a handler, delete it first
470 m_handler
= new wxScrollHelperEvtHandler(this);
471 m_targetWindow
->PushEventHandler(m_handler
);
475 void wxScrollHelperBase::SetTargetWindow(wxWindow
*target
)
477 wxCHECK_RET( target
, wxT("target window must not be NULL") );
479 if ( target
== m_targetWindow
)
482 DoSetTargetWindow(target
);
485 wxWindow
*wxScrollHelperBase::GetTargetWindow() const
487 return m_targetWindow
;
490 // ----------------------------------------------------------------------------
491 // scrolling implementation itself
492 // ----------------------------------------------------------------------------
494 void wxScrollHelperBase::HandleOnScroll(wxScrollWinEvent
& event
)
496 int nScrollInc
= CalcScrollInc(event
);
497 if ( nScrollInc
== 0 )
499 // can't scroll further
505 bool needsRefresh
= false;
508 int orient
= event
.GetOrientation();
509 if (orient
== wxHORIZONTAL
)
511 if ( m_xScrollingEnabled
)
513 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
522 if ( m_yScrollingEnabled
)
524 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
534 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
535 // otherwise invalidated area could be updated incorrectly later when
536 // ScrollWindow() makes sure they're repainted before scrolling them
538 // wxWindowMac is taking care of making sure the update area is correctly
539 // set up, while not forcing an immediate redraw
541 m_targetWindow
->Update();
545 if (orient
== wxHORIZONTAL
)
547 m_xScrollPosition
+= nScrollInc
;
548 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
552 m_yScrollPosition
+= nScrollInc
;
553 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
558 m_targetWindow
->Refresh(true, GetScrollRect());
562 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
566 int wxScrollHelperBase::CalcScrollInc(wxScrollWinEvent
& event
)
568 int pos
= event
.GetPosition();
569 int orient
= event
.GetOrientation();
572 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
574 if (orient
== wxHORIZONTAL
)
575 nScrollInc
= - m_xScrollPosition
;
577 nScrollInc
= - m_yScrollPosition
;
579 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
581 if (orient
== wxHORIZONTAL
)
582 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
584 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
586 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
590 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
594 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
596 if (orient
== wxHORIZONTAL
)
597 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
599 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
601 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
603 if (orient
== wxHORIZONTAL
)
604 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
606 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
608 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
609 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
611 if (orient
== wxHORIZONTAL
)
612 nScrollInc
= pos
- m_xScrollPosition
;
614 nScrollInc
= pos
- m_yScrollPosition
;
617 if (orient
== wxHORIZONTAL
)
619 if ( m_xScrollPosition
+ nScrollInc
< 0 )
621 // As -ve as we can go
622 nScrollInc
= -m_xScrollPosition
;
624 else // check for the other bound
626 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
627 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
629 // As +ve as we can go
630 nScrollInc
= posMax
- m_xScrollPosition
;
636 if ( m_yScrollPosition
+ nScrollInc
< 0 )
638 // As -ve as we can go
639 nScrollInc
= -m_yScrollPosition
;
641 else // check for the other bound
643 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
644 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
646 // As +ve as we can go
647 nScrollInc
= posMax
- m_yScrollPosition
;
655 void wxScrollHelperBase::DoPrepareDC(wxDC
& dc
)
657 wxPoint pt
= dc
.GetDeviceOrigin();
659 // It may actually be correct to always query
660 // the m_sign from the DC here, but I leave the
661 // #ifdef GTK for now.
662 if (m_win
->GetLayoutDirection() == wxLayout_RightToLeft
)
663 dc
.SetDeviceOrigin( pt
.x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
,
664 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
667 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
668 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
669 dc
.SetUserScale( m_scaleX
, m_scaleY
);
672 void wxScrollHelperBase::SetScrollRate( int xstep
, int ystep
)
674 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
675 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
677 m_xScrollPixelsPerLine
= xstep
;
678 m_yScrollPixelsPerLine
= ystep
;
680 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
681 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
683 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
684 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
685 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
690 void wxScrollHelperBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
693 *x_unit
= m_xScrollPixelsPerLine
;
695 *y_unit
= m_yScrollPixelsPerLine
;
699 int wxScrollHelperBase::GetScrollLines( int orient
) const
701 if ( orient
== wxHORIZONTAL
)
702 return m_xScrollLines
;
704 return m_yScrollLines
;
707 int wxScrollHelperBase::GetScrollPageSize(int orient
) const
709 if ( orient
== wxHORIZONTAL
)
710 return m_xScrollLinesPerPage
;
712 return m_yScrollLinesPerPage
;
715 void wxScrollHelperBase::SetScrollPageSize(int orient
, int pageSize
)
717 if ( orient
== wxHORIZONTAL
)
718 m_xScrollLinesPerPage
= pageSize
;
720 m_yScrollLinesPerPage
= pageSize
;
723 void wxScrollHelperBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
725 m_xScrollingEnabled
= x_scroll
;
726 m_yScrollingEnabled
= y_scroll
;
729 // Where the current view starts from
730 void wxScrollHelperBase::DoGetViewStart (int *x
, int *y
) const
733 *x
= m_xScrollPosition
;
735 *y
= m_yScrollPosition
;
738 void wxScrollHelperBase::DoCalcScrolledPosition(int x
, int y
,
739 int *xx
, int *yy
) const
742 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
744 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
747 void wxScrollHelperBase::DoCalcUnscrolledPosition(int x
, int y
,
748 int *xx
, int *yy
) const
751 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
753 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
756 // ----------------------------------------------------------------------------
758 // ----------------------------------------------------------------------------
760 bool wxScrollHelperBase::ScrollLayout()
762 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
764 // If we're the scroll target, take into account the
765 // virtual size and scrolled position of the window.
767 int x
= 0, y
= 0, w
= 0, h
= 0;
768 CalcScrolledPosition(0,0, &x
,&y
);
769 m_win
->GetVirtualSize(&w
, &h
);
770 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
774 // fall back to default for LayoutConstraints
775 return m_win
->wxWindow::Layout();
778 void wxScrollHelperBase::ScrollDoSetVirtualSize(int x
, int y
)
780 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
783 if (m_win
->GetAutoLayout())
787 // wxWindow's GetBestVirtualSize returns the actual window size,
788 // whereas we want to return the virtual size
789 wxSize
wxScrollHelperBase::ScrollGetBestVirtualSize() const
791 wxSize
clientSize(m_win
->GetClientSize());
792 if ( m_win
->GetSizer() )
793 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
798 // ----------------------------------------------------------------------------
800 // ----------------------------------------------------------------------------
802 // Default OnSize resets scrollbars, if any
803 void wxScrollHelperBase::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
805 if ( m_targetWindow
->GetAutoLayout() )
807 wxSize size
= m_targetWindow
->GetBestVirtualSize();
809 // This will call ::Layout() and ::AdjustScrollbars()
810 m_win
->SetVirtualSize( size
);
818 // This calls OnDraw, having adjusted the origin according to the current
820 void wxScrollHelperBase::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
822 // don't use m_targetWindow here, this is always called for ourselves
829 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
830 // compatibility here - if we used OnKeyDown(), the programs which process
831 // arrows themselves in their OnChar() would never get the message and like
832 // this they always have the priority
833 void wxScrollHelperBase::HandleOnChar(wxKeyEvent
& event
)
835 if ( !m_kbdScrollingEnabled
)
841 // prepare the event this key press maps to
842 wxScrollWinEvent newEvent
;
844 newEvent
.SetPosition(0);
845 newEvent
.SetEventObject(m_win
);
846 newEvent
.SetId(m_win
->GetId());
848 // this is the default, it's changed to wxHORIZONTAL below if needed
849 newEvent
.SetOrientation(wxVERTICAL
);
851 // some key events result in scrolling in both horizontal and vertical
852 // direction, e.g. Ctrl-{Home,End}, if this flag is true we should generate
853 // a second event in horizontal direction in addition to the primary one
854 bool sendHorizontalToo
= false;
856 switch ( event
.GetKeyCode() )
859 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
863 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
867 newEvent
.SetEventType(wxEVT_SCROLLWIN_TOP
);
869 sendHorizontalToo
= event
.ControlDown();
873 newEvent
.SetEventType(wxEVT_SCROLLWIN_BOTTOM
);
875 sendHorizontalToo
= event
.ControlDown();
879 newEvent
.SetOrientation(wxHORIZONTAL
);
883 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
887 newEvent
.SetOrientation(wxHORIZONTAL
);
891 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
895 // not a scrolling key
900 m_win
->ProcessWindowEvent(newEvent
);
902 if ( sendHorizontalToo
)
904 newEvent
.SetOrientation(wxHORIZONTAL
);
905 m_win
->ProcessWindowEvent(newEvent
);
909 // ----------------------------------------------------------------------------
910 // autoscroll stuff: these functions deal with sending fake scroll events when
911 // a captured mouse is being held outside the window
912 // ----------------------------------------------------------------------------
914 bool wxScrollHelperBase::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
916 // only send the event if the window is scrollable in this direction
917 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
918 return win
->HasScrollbar(event
.GetOrientation());
921 void wxScrollHelperBase::StopAutoScrolling()
924 wxDELETE(m_timerAutoScroll
);
928 void wxScrollHelperBase::HandleOnMouseEnter(wxMouseEvent
& event
)
935 void wxScrollHelperBase::HandleOnMouseLeave(wxMouseEvent
& event
)
937 // don't prevent the usual processing of the event from taking place
940 // when a captured mouse leave a scrolled window we start generate
941 // scrolling events to allow, for example, extending selection beyond the
942 // visible area in some controls
943 if ( wxWindow::GetCapture() == m_targetWindow
)
945 // where is the mouse leaving?
947 wxPoint pt
= event
.GetPosition();
950 orient
= wxHORIZONTAL
;
958 else // we're lower or to the right of the window
960 wxSize size
= m_targetWindow
->GetClientSize();
963 orient
= wxHORIZONTAL
;
964 pos
= m_xScrollLines
;
966 else if ( pt
.y
> size
.y
)
969 pos
= m_yScrollLines
;
971 else // this should be impossible
973 // but seems to happen sometimes under wxMSW - maybe it's a bug
974 // there but for now just ignore it
976 //wxFAIL_MSG( wxT("can't understand where has mouse gone") );
982 // only start the auto scroll timer if the window can be scrolled in
984 if ( !m_targetWindow
->HasScrollbar(orient
) )
988 delete m_timerAutoScroll
;
989 m_timerAutoScroll
= new wxAutoScrollTimer
991 m_targetWindow
, this,
992 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
993 : wxEVT_SCROLLWIN_LINEDOWN
,
997 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1004 #if wxUSE_MOUSEWHEEL
1006 void wxScrollHelperBase::HandleOnMouseWheel(wxMouseEvent
& event
)
1008 m_wheelRotation
+= event
.GetWheelRotation();
1009 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1010 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1015 wxScrollWinEvent newEvent
;
1017 newEvent
.SetPosition(0);
1018 newEvent
.SetOrientation( event
.GetWheelAxis() == 0 ? wxVERTICAL
: wxHORIZONTAL
);
1019 newEvent
.SetEventObject(m_win
);
1021 if (event
.IsPageScroll())
1024 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1026 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1028 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1032 lines
*= event
.GetLinesPerAction();
1034 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1036 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1038 int times
= abs(lines
);
1039 for (; times
> 0; times
--)
1040 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1045 #endif // wxUSE_MOUSEWHEEL
1047 void wxScrollHelperBase::HandleOnChildFocus(wxChildFocusEvent
& event
)
1049 // this event should be processed by all windows in parenthood chain,
1050 // e.g. so that nested wxScrolledWindows work correctly
1053 // find the immediate child under which the window receiving focus is:
1054 wxWindow
*win
= event
.GetWindow();
1056 if ( win
== m_targetWindow
)
1057 return; // nothing to do
1059 #if defined( __WXOSX__ ) && wxUSE_SCROLLBAR
1060 if (wxDynamicCast(win
, wxScrollBar
))
1064 // Fixing ticket: http://trac.wxwidgets.org/ticket/9563
1065 // When a child inside a wxControlContainer receives a focus, the
1066 // wxControlContainer generates an artificial wxChildFocusEvent for
1067 // itself, telling its parent that 'it' received the focus. The effect is
1068 // that this->HandleOnChildFocus is called twice, first with the
1069 // artificial wxChildFocusEvent and then with the original event. We need
1070 // to ignore the artificial event here or otherwise HandleOnChildFocus
1071 // would first scroll the target window to make the entire
1072 // wxControlContainer visible and immediately afterwards scroll the target
1073 // window again to make the child widget visible. This leads to ugly
1074 // flickering when using nested wxPanels/wxScrolledWindows.
1076 // Ignore this event if 'win' is derived from wxControlContainer AND its
1077 // parent is the m_targetWindow AND 'win' is not actually reciving the
1078 // focus (win != FindFocus). TODO: This affects all wxControlContainer
1079 // objects, but wxControlContainer is not part of the wxWidgets RTTI and
1080 // so wxDynamicCast(win, wxControlContainer) does not compile. Find a way
1081 // to determine if 'win' derives from wxControlContainer. Until then,
1082 // testing if 'win' derives from wxPanel will probably get >90% of all
1085 wxWindow
*actual_focus
=wxWindow::FindFocus();
1086 if (win
!= actual_focus
&&
1087 wxDynamicCast(win
, wxPanel
) != 0 &&
1088 win
->GetParent() == m_targetWindow
)
1089 // if win is a wxPanel and receives the focus, it should not be
1090 // scrolled into view
1093 const wxRect
viewRect(m_targetWindow
->GetClientRect());
1095 // For composite controls such as wxComboCtrl we should try to fit the
1096 // entire control inside the visible area of the target window, not just
1097 // the focused child of the control. Otherwise we'd make only the textctrl
1098 // part of a wxComboCtrl visible and the button would still be outside the
1099 // scrolled area. But do so only if the parent fits *entirely* inside the
1100 // scrolled window. In other situations, such as nested wxPanel or
1101 // wxScrolledWindows, the parent might be way too big to fit inside the
1102 // scrolled window. If that is the case, then make only the focused window
1104 if ( win
->GetParent() != m_targetWindow
)
1106 wxWindow
*parent
=win
->GetParent();
1107 wxSize parent_size
=parent
->GetSize();
1108 if (parent_size
.GetWidth() <= viewRect
.GetWidth() &&
1109 parent_size
.GetHeight() <= viewRect
.GetHeight())
1110 // make the immediate parent visible instead of the focused control
1114 // make win position relative to the m_targetWindow viewing area instead of
1117 winRect(m_targetWindow
->ScreenToClient(win
->GetScreenPosition()),
1120 // check if it's fully visible
1121 if ( viewRect
.Contains(winRect
) )
1123 // it is, nothing to do
1127 // check if we can make it fully visible: this is only possible if it's not
1128 // larger than our view area
1129 if ( winRect
.GetWidth() > viewRect
.GetWidth() ||
1130 winRect
.GetHeight() > viewRect
.GetHeight() )
1132 // we can't make it fit so avoid scrolling it at all, this is only
1133 // going to be confusing and not helpful
1138 // do make the window fit inside the view area by scrolling to it
1140 GetScrollPixelsPerUnit(&stepx
, &stepy
);
1143 GetViewStart(&startx
, &starty
);
1145 // first in vertical direction:
1150 if ( winRect
.GetTop() < 0 )
1152 diff
= winRect
.GetTop();
1154 else if ( winRect
.GetBottom() > viewRect
.GetHeight() )
1156 diff
= winRect
.GetBottom() - viewRect
.GetHeight() + 1;
1157 // round up to next scroll step if we can't get exact position,
1158 // so that the window is fully visible:
1162 starty
= (starty
* stepy
+ diff
) / stepy
;
1170 if ( winRect
.GetLeft() < 0 )
1172 diff
= winRect
.GetLeft();
1174 else if ( winRect
.GetRight() > viewRect
.GetWidth() )
1176 diff
= winRect
.GetRight() - viewRect
.GetWidth() + 1;
1177 // round up to next scroll step if we can't get exact position,
1178 // so that the window is fully visible:
1182 startx
= (startx
* stepx
+ diff
) / stepx
;
1185 Scroll(startx
, starty
);
1189 #ifdef wxHAS_GENERIC_SCROLLWIN
1191 // ----------------------------------------------------------------------------
1192 // wxScrollHelper implementation
1193 // ----------------------------------------------------------------------------
1195 wxScrollHelper::wxScrollHelper(wxWindow
*winToScroll
)
1196 : wxScrollHelperBase(winToScroll
)
1199 m_yVisibility
= wxSHOW_SB_DEFAULT
;
1202 void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz
,
1203 wxScrollbarVisibility vert
)
1205 if ( horz
!= m_xVisibility
|| vert
!= m_yVisibility
)
1207 m_xVisibility
= horz
;
1208 m_yVisibility
= vert
;
1215 wxScrollHelper::DoAdjustScrollbar(int orient
,
1220 int& scrollPosition
,
1221 int& scrollLinesPerPage
,
1222 wxScrollbarVisibility visibility
)
1224 // scroll lines per page: if 0, no scrolling is needed
1225 // check if we need scrollbar in this direction at all
1226 if ( pixelsPerUnit
== 0 || clientSize
>= virtSize
)
1228 // scrolling is disabled or unnecessary
1231 scrollLinesPerPage
= 0;
1233 else // might need scrolling
1235 // Round up integer division to catch any "leftover" client space.
1236 scrollUnits
= (virtSize
+ pixelsPerUnit
- 1) / pixelsPerUnit
;
1238 // Calculate the number of fully scroll units
1239 scrollLinesPerPage
= clientSize
/ pixelsPerUnit
;
1241 if ( scrollLinesPerPage
>= scrollUnits
)
1243 // we're big enough to not need scrolling
1246 scrollLinesPerPage
= 0;
1248 else // we do need a scrollbar
1250 if ( scrollLinesPerPage
< 1 )
1251 scrollLinesPerPage
= 1;
1253 // Correct position if greater than extent of canvas minus
1254 // the visible portion of it or if below zero
1255 const int posMax
= scrollUnits
- scrollLinesPerPage
;
1256 if ( scrollPosition
> posMax
)
1257 scrollPosition
= posMax
;
1258 else if ( scrollPosition
< 0 )
1263 // in wxSHOW_SB_NEVER case don't show the scrollbar even if it's needed, in
1264 // wxSHOW_SB_ALWAYS case show the scrollbar even if it's not needed by
1265 // passing a special range value to SetScrollbar()
1267 switch ( visibility
)
1269 case wxSHOW_SB_NEVER
:
1273 case wxSHOW_SB_ALWAYS
:
1274 range
= scrollUnits
? scrollUnits
: -1;
1278 wxFAIL_MSG( wxS("unknown scrollbar visibility") );
1281 case wxSHOW_SB_DEFAULT
:
1282 range
= scrollUnits
;
1287 m_win
->SetScrollbar(orient
, scrollPosition
, scrollLinesPerPage
, range
);
1290 void wxScrollHelper::AdjustScrollbars()
1292 static wxRecursionGuardFlag s_flagReentrancy
;
1293 wxRecursionGuard
guard(s_flagReentrancy
);
1294 if ( guard
.IsInside() )
1296 // don't reenter AdjustScrollbars() while another call to
1297 // AdjustScrollbars() is in progress because this may lead to calling
1298 // ScrollWindow() twice and this can really happen under MSW if
1299 // SetScrollbar() call below adds or removes the scrollbar which
1300 // changes the window size and hence results in another
1301 // AdjustScrollbars() call
1305 int oldXScroll
= m_xScrollPosition
;
1306 int oldYScroll
= m_yScrollPosition
;
1308 // we may need to readjust the scrollbars several times as enabling one of
1309 // them reduces the area available for the window contents and so can make
1310 // the other scrollbar necessary now although it wasn't necessary before
1312 // VZ: normally this loop should be over in at most 2 iterations, I don't
1313 // know why do we need 5 of them
1314 for ( int iterationCount
= 0; iterationCount
< 5; iterationCount
++ )
1316 wxSize clientSize
= GetTargetSize();
1317 const wxSize virtSize
= m_targetWindow
->GetVirtualSize();
1319 // this block of code tries to work around the following problem: the
1320 // window could have been just resized to have enough space to show its
1321 // full contents without the scrollbars, but its client size could be
1322 // not big enough because it does have the scrollbars right now and so
1323 // the scrollbars would remain even though we don't need them any more
1325 // to prevent this from happening, check if we have enough space for
1326 // everything without the scrollbars and explicitly disable them then
1327 const wxSize availSize
= GetSizeAvailableForScrollTarget(
1328 m_win
->GetSize() - m_win
->GetWindowBorderSize());
1329 if ( availSize
!= clientSize
)
1331 if ( availSize
.x
>= virtSize
.x
&& availSize
.y
>= virtSize
.y
)
1333 // this will be enough to make the scrollbars disappear below
1334 // and then the client size will indeed become equal to the
1335 // full available size
1336 clientSize
= availSize
;
1341 DoAdjustScrollbar(wxHORIZONTAL
,
1344 m_xScrollPixelsPerLine
,
1347 m_xScrollLinesPerPage
,
1350 DoAdjustScrollbar(wxVERTICAL
,
1353 m_yScrollPixelsPerLine
,
1356 m_yScrollLinesPerPage
,
1360 // If a scrollbar (dis)appeared as a result of this, we need to adjust
1361 // them again but if the client size didn't change, then we're done
1362 if ( GetTargetSize() == clientSize
)
1367 // Sorry, some Motif-specific code to implement a backing pixmap
1368 // for the wxRETAINED style. Implementing a backing store can't
1369 // be entirely generic because it relies on the wxWindowDC implementation
1370 // to duplicate X drawing calls for the backing pixmap.
1372 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
1374 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
1376 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
1377 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
1378 if (m_targetWindow
->GetBackingPixmap() &&
1379 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
1380 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
1382 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
1383 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
1386 if (!m_targetWindow
->GetBackingPixmap() &&
1387 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
1389 int depth
= wxDisplayDepth();
1390 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
1391 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
1392 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
1393 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
1399 if (oldXScroll
!= m_xScrollPosition
)
1401 if (m_xScrollingEnabled
)
1402 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
1405 m_targetWindow
->Refresh(true, GetScrollRect());
1408 if (oldYScroll
!= m_yScrollPosition
)
1410 if (m_yScrollingEnabled
)
1411 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
1414 m_targetWindow
->Refresh(true, GetScrollRect());
1418 void wxScrollHelper::DoScroll( int x_pos
, int y_pos
)
1420 if (!m_targetWindow
)
1423 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
1424 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
1427 GetTargetSize(&w
, &h
);
1429 // compute new position:
1430 int new_x
= m_xScrollPosition
;
1431 int new_y
= m_yScrollPosition
;
1433 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
1437 // Calculate page size i.e. number of scroll units you get on the
1438 // current client window
1439 int noPagePositions
= w
/m_xScrollPixelsPerLine
;
1440 if (noPagePositions
< 1) noPagePositions
= 1;
1442 // Correct position if greater than extent of canvas minus
1443 // the visible portion of it or if below zero
1444 new_x
= wxMin( m_xScrollLines
-noPagePositions
, new_x
);
1445 new_x
= wxMax( 0, new_x
);
1447 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
1451 // Calculate page size i.e. number of scroll units you get on the
1452 // current client window
1453 int noPagePositions
= h
/m_yScrollPixelsPerLine
;
1454 if (noPagePositions
< 1) noPagePositions
= 1;
1456 // Correct position if greater than extent of canvas minus
1457 // the visible portion of it or if below zero
1458 new_y
= wxMin( m_yScrollLines
-noPagePositions
, new_y
);
1459 new_y
= wxMax( 0, new_y
);
1462 if ( new_x
== m_xScrollPosition
&& new_y
== m_yScrollPosition
)
1463 return; // nothing to do, the position didn't change
1465 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
1466 // otherwise invalidated area could be updated incorrectly later when
1467 // ScrollWindow() makes sure they're repainted before scrolling them
1468 m_targetWindow
->Update();
1470 // update the position and scroll the window now:
1471 if (m_xScrollPosition
!= new_x
)
1473 int old_x
= m_xScrollPosition
;
1474 m_xScrollPosition
= new_x
;
1475 m_win
->SetScrollPos( wxHORIZONTAL
, new_x
);
1476 m_targetWindow
->ScrollWindow( (old_x
-new_x
)*m_xScrollPixelsPerLine
, 0,
1480 if (m_yScrollPosition
!= new_y
)
1482 int old_y
= m_yScrollPosition
;
1483 m_yScrollPosition
= new_y
;
1484 m_win
->SetScrollPos( wxVERTICAL
, new_y
);
1485 m_targetWindow
->ScrollWindow( 0, (old_y
-new_y
)*m_yScrollPixelsPerLine
,
1490 #endif // wxHAS_GENERIC_SCROLLWIN
1492 // ----------------------------------------------------------------------------
1493 // wxScrolled<T> and wxScrolledWindow implementation
1494 // ----------------------------------------------------------------------------
1496 wxSize
wxScrolledT_Helper::FilterBestSize(const wxWindow
*win
,
1497 const wxScrollHelper
*helper
,
1498 const wxSize
& origBest
)
1500 // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1501 // all scrollable windows should behave like this, only those that
1502 // contain children controls within scrollable area
1503 // (i.e., wxScrolledWindow) and other some scrollable windows may
1504 // have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1506 wxSize best
= origBest
;
1508 if ( win
->GetAutoLayout() )
1510 // Only use the content to set the window size in the direction
1511 // where there's no scrolling; otherwise we're going to get a huge
1512 // window in the direction in which scrolling is enabled
1514 helper
->GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1516 // NB: This code used to use *current* size if min size wasn't
1517 // specified, presumably to get some reasonable (i.e., larger than
1518 // minimal) size. But that's a wrong thing to do in GetBestSize(),
1519 // so we use minimal size as specified. If the app needs some
1520 // minimal size for its scrolled window, it should set it and put
1521 // the window into sizer as expandable so that it can use all space
1524 // See also http://svn.wxwidgets.org/viewvc/wx?view=rev&revision=45864
1526 wxSize minSize
= win
->GetMinSize();
1529 best
.x
= minSize
.x
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1532 best
.y
= minSize
.y
+ wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
1539 WXLRESULT
wxScrolledT_Helper::FilterMSWWindowProc(WXUINT nMsg
, WXLRESULT rc
)
1542 // we need to process arrows ourselves for scrolling
1543 if ( nMsg
== WM_GETDLGCODE
)
1545 rc
|= DLGC_WANTARROWS
;
1552 // NB: skipping wxScrolled<T> in wxRTTI information because being a templte,
1553 // it doesn't and can't implement wxRTTI support
1554 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)