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.
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/scrolwin.h"
32 #include "wx/dcclient.h"
35 #include "wx/settings.h"
39 #include "wx/scrolbar.h"
42 #include "wx/recguard.h"
45 #include <windows.h> // for DLGC_WANTARROWS
46 #include "wx/msw/winundef.h"
50 // For wxRETAINED implementation
51 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
52 //This code switches off the compiler warnings
53 # pragma message disable nosimpint
57 # pragma message enable nosimpint
63 style wxHSCROLL | wxVSCROLL
66 // ----------------------------------------------------------------------------
67 // wxScrollHelperEvtHandler: intercept the events from the window and forward
68 // them to wxScrollHelper
69 // ----------------------------------------------------------------------------
71 class WXDLLEXPORT wxScrollHelperEvtHandler
: public wxEvtHandler
74 wxScrollHelperEvtHandler(wxScrollHelperBase
*scrollHelper
)
76 m_scrollHelper
= scrollHelper
;
79 virtual bool ProcessEvent(wxEvent
& event
);
82 wxScrollHelperBase
*m_scrollHelper
;
84 wxDECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
);
88 // ----------------------------------------------------------------------------
89 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
90 // a captured mouse is held outside the window
91 // ----------------------------------------------------------------------------
93 class wxAutoScrollTimer
: public wxTimer
96 wxAutoScrollTimer(wxWindow
*winToScroll
,
97 wxScrollHelperBase
*scroll
,
98 wxEventType eventTypeToSend
,
101 virtual void Notify();
105 wxScrollHelperBase
*m_scrollHelper
;
106 wxEventType m_eventType
;
110 wxDECLARE_NO_COPY_CLASS(wxAutoScrollTimer
);
113 // ============================================================================
115 // ============================================================================
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
122 wxScrollHelperBase
*scroll
,
123 wxEventType eventTypeToSend
,
127 m_scrollHelper
= scroll
;
128 m_eventType
= eventTypeToSend
;
133 void wxAutoScrollTimer::Notify()
135 // only do all this as long as the window is capturing the mouse
136 if ( wxWindow::GetCapture() != m_win
)
140 else // we still capture the mouse, continue generating events
142 // first scroll the window if we are allowed to do it
143 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
144 event1
.SetEventObject(m_win
);
145 event1
.SetId(m_win
->GetId());
146 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
147 m_win
->GetEventHandler()->ProcessEvent(event1
) )
149 // and then send a pseudo mouse-move event to refresh the selection
150 wxMouseEvent
event2(wxEVT_MOTION
);
151 event2
.SetPosition(wxGetMousePosition());
153 // the mouse event coordinates should be client, not screen as
154 // returned by wxGetMousePosition
155 wxWindow
*parentTop
= m_win
;
156 while ( parentTop
->GetParent() )
157 parentTop
= parentTop
->GetParent();
158 wxPoint ptOrig
= parentTop
->GetPosition();
159 event2
.m_x
-= ptOrig
.x
;
160 event2
.m_y
-= ptOrig
.y
;
162 event2
.SetEventObject(m_win
);
164 wxMouseState mouseState
= wxGetMouseState();
166 event2
.m_leftDown
= mouseState
.LeftIsDown();
167 event2
.m_middleDown
= mouseState
.MiddleIsDown();
168 event2
.m_rightDown
= mouseState
.RightIsDown();
170 event2
.m_shiftDown
= mouseState
.ShiftDown();
171 event2
.m_controlDown
= mouseState
.ControlDown();
172 event2
.m_altDown
= mouseState
.AltDown();
173 event2
.m_metaDown
= mouseState
.MetaDown();
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 // Pass it on to the real handler: notice that we must not call
194 // ProcessEvent() on this object itself as it wouldn't pass it to the next
195 // handler (i.e. the real window) if we're called from a previous handler
196 // (as indicated by "process here only" flag being set) and we do want to
197 // execute the handler defined in the window we're associated with right
198 // now, without waiting until TryAfter() is called from wxEvtHandler.
199 bool processed
= m_nextHandler
->ProcessEvent(event
);
201 // always process the size events ourselves, even if the user code handles
202 // them as well, as we need to AdjustScrollbars()
204 // NB: it is important to do it after processing the event in the normal
205 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
206 // scrollbar[s] (dis)appear and it should be seen by the user code
208 if ( evType
== wxEVT_SIZE
)
210 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
214 if ( processed
&& event
.IsCommandEvent())
217 // For wxEVT_PAINT the user code can either handle this event as usual or
218 // override virtual OnDraw(), so if the event hasn't been handled we need
219 // to call this virtual function ourselves.
221 #ifndef __WXUNIVERSAL__
222 // in wxUniversal "processed" will always be true, because
223 // all windows use the paint event to draw themselves.
224 // In this case we can't use this flag to determine if a custom
225 // paint event handler already drew our window and we just
226 // call OnDraw() anyway.
228 #endif // !__WXUNIVERSAL__
229 evType
== wxEVT_PAINT
)
231 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
235 if ( evType
== wxEVT_CHILD_FOCUS
)
237 m_scrollHelper
->HandleOnChildFocus((wxChildFocusEvent
&)event
);
241 // reset the skipped flag (which might have been set to true in
242 // ProcessEvent() above) to be able to test it below
243 bool wasSkipped
= event
.GetSkipped();
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 if ( !event
.GetSkipped() )
259 // it makes sense to indicate that we processed the message as we
260 // did scroll the window (and also notice that wxAutoScrollTimer
261 // relies on our return value to stop scrolling when we are at top
262 // or bottom already)
268 if ( evType
== wxEVT_ENTER_WINDOW
)
270 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
272 else if ( evType
== wxEVT_LEAVE_WINDOW
)
274 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
277 // Use GTK's own scroll wheel handling in GtkScrolledWindow
279 else if ( evType
== wxEVT_MOUSEWHEEL
)
281 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
285 #endif // wxUSE_MOUSEWHEEL
286 else if ( evType
== wxEVT_CHAR
)
288 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
289 if ( !event
.GetSkipped() )
296 event
.Skip(wasSkipped
);
298 // We called ProcessEvent() on the next handler, meaning that we explicitly
299 // worked around the request to process the event in this handler only. As
300 // explained above, this is unfortunately really necessary but the trouble
301 // is that the event will continue to be post-processed by the previous
302 // handler resulting in duplicate calls to event handlers. Call the special
303 // function below to prevent this from happening, base class DoTryChain()
304 // will check for it and behave accordingly.
306 // And if we're not called from DoTryChain(), this won't do anything anyhow.
307 event
.DidntHonourProcessOnlyIn();
312 // ============================================================================
313 // wxScrollHelperBase implementation
314 // ============================================================================
316 // ----------------------------------------------------------------------------
317 // wxScrollHelperBase construction
318 // ----------------------------------------------------------------------------
320 wxScrollHelperBase::wxScrollHelperBase(wxWindow
*win
)
322 wxASSERT_MSG( win
, wxT("associated window can't be NULL in wxScrollHelper") );
324 m_xScrollPixelsPerLine
=
325 m_yScrollPixelsPerLine
=
330 m_xScrollLinesPerPage
=
331 m_yScrollLinesPerPage
= 0;
333 m_xScrollingEnabled
=
334 m_yScrollingEnabled
= true;
336 m_kbdScrollingEnabled
= true;
345 m_targetWindow
= NULL
;
347 m_timerAutoScroll
= NULL
;
353 m_win
->SetScrollHelper(static_cast<wxScrollHelper
*>(this));
355 // by default, the associated window is also the target window
356 DoSetTargetWindow(win
);
359 wxScrollHelperBase::~wxScrollHelperBase()
366 // ----------------------------------------------------------------------------
367 // setting scrolling parameters
368 // ----------------------------------------------------------------------------
370 void wxScrollHelperBase::SetScrollbars(int pixelsPerUnitX
,
378 // Convert positions expressed in scroll units to positions in pixels.
379 int xPosInPixels
= (xPos
+ m_xScrollPosition
)*m_xScrollPixelsPerLine
,
380 yPosInPixels
= (yPos
+ m_yScrollPosition
)*m_yScrollPixelsPerLine
;
384 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
385 (noUnitsX
< m_xScrollLines
&& xPosInPixels
> pixelsPerUnitX
* noUnitsX
) ||
387 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
388 (noUnitsY
< m_yScrollLines
&& yPosInPixels
> pixelsPerUnitY
* noUnitsY
) ||
389 (xPos
!= m_xScrollPosition
) ||
390 (yPos
!= m_yScrollPosition
)
393 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
394 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
395 m_xScrollPosition
= xPos
;
396 m_yScrollPosition
= yPos
;
398 int w
= noUnitsX
* pixelsPerUnitX
;
399 int h
= noUnitsY
* pixelsPerUnitY
;
401 // For better backward compatibility we set persisting limits
402 // here not just the size. It makes SetScrollbars 'sticky'
403 // emulating the old non-autoscroll behaviour.
404 // m_targetWindow->SetVirtualSizeHints( w, h );
406 // The above should arguably be deprecated, this however we still need.
408 // take care not to set 0 virtual size, 0 means that we don't have any
409 // scrollbars and hence we should use the real size instead of the virtual
410 // one which is indicated by using wxDefaultCoord
411 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
412 h
? h
: wxDefaultCoord
);
414 if (do_refresh
&& !noRefresh
)
415 m_targetWindow
->Refresh(true, GetScrollRect());
417 #ifndef __WXUNIVERSAL__
418 // If the target is not the same as the window with the scrollbars,
419 // then we need to update the scrollbars here, since they won't have
420 // been updated by SetVirtualSize().
421 if ( m_targetWindow
!= m_win
)
422 #endif // !__WXUNIVERSAL__
426 #ifndef __WXUNIVERSAL__
429 // otherwise this has been done by AdjustScrollbars, above
431 #endif // !__WXUNIVERSAL__
434 // ----------------------------------------------------------------------------
435 // [target] window handling
436 // ----------------------------------------------------------------------------
438 void wxScrollHelperBase::DeleteEvtHandler()
440 // search for m_handler in the handler list
441 if ( m_win
&& m_handler
)
443 if ( m_win
->RemoveEventHandler(m_handler
) )
447 //else: something is very wrong, so better [maybe] leak memory than
448 // risk a crash because of double deletion
454 void wxScrollHelperBase::DoSetTargetWindow(wxWindow
*target
)
456 m_targetWindow
= target
;
458 target
->MacSetClipChildren( true ) ;
461 // install the event handler which will intercept the events we're
462 // interested in (but only do it for our real window, not the target window
463 // which we scroll - we don't need to hijack its events)
464 if ( m_targetWindow
== m_win
)
466 // if we already have a handler, delete it first
469 m_handler
= new wxScrollHelperEvtHandler(this);
470 m_targetWindow
->PushEventHandler(m_handler
);
474 void wxScrollHelperBase::SetTargetWindow(wxWindow
*target
)
476 wxCHECK_RET( target
, wxT("target window must not be NULL") );
478 if ( target
== m_targetWindow
)
481 DoSetTargetWindow(target
);
484 wxWindow
*wxScrollHelperBase::GetTargetWindow() const
486 return m_targetWindow
;
489 // ----------------------------------------------------------------------------
490 // scrolling implementation itself
491 // ----------------------------------------------------------------------------
493 void wxScrollHelperBase::HandleOnScroll(wxScrollWinEvent
& event
)
495 int nScrollInc
= CalcScrollInc(event
);
496 if ( nScrollInc
== 0 )
498 // can't scroll further
504 bool needsRefresh
= false;
507 int orient
= event
.GetOrientation();
508 if (orient
== wxHORIZONTAL
)
510 if ( m_xScrollingEnabled
)
512 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
521 if ( m_yScrollingEnabled
)
523 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
533 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
534 // otherwise invalidated area could be updated incorrectly later when
535 // ScrollWindow() makes sure they're repainted before scrolling them
537 // wxWindowMac is taking care of making sure the update area is correctly
538 // set up, while not forcing an immediate redraw
540 m_targetWindow
->Update();
544 if (orient
== wxHORIZONTAL
)
546 m_xScrollPosition
+= nScrollInc
;
547 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
551 m_yScrollPosition
+= nScrollInc
;
552 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
557 m_targetWindow
->Refresh(true, GetScrollRect());
561 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
565 int wxScrollHelperBase::CalcScrollInc(wxScrollWinEvent
& event
)
567 int pos
= event
.GetPosition();
568 int orient
= event
.GetOrientation();
571 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
573 if (orient
== wxHORIZONTAL
)
574 nScrollInc
= - m_xScrollPosition
;
576 nScrollInc
= - m_yScrollPosition
;
578 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
580 if (orient
== wxHORIZONTAL
)
581 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
583 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
585 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
589 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
593 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
595 if (orient
== wxHORIZONTAL
)
596 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
598 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
600 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
602 if (orient
== wxHORIZONTAL
)
603 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
605 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
607 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
608 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
610 if (orient
== wxHORIZONTAL
)
611 nScrollInc
= pos
- m_xScrollPosition
;
613 nScrollInc
= pos
- m_yScrollPosition
;
616 if (orient
== wxHORIZONTAL
)
618 if ( m_xScrollPosition
+ nScrollInc
< 0 )
620 // As -ve as we can go
621 nScrollInc
= -m_xScrollPosition
;
623 else // check for the other bound
625 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
626 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
628 // As +ve as we can go
629 nScrollInc
= posMax
- m_xScrollPosition
;
635 if ( m_yScrollPosition
+ nScrollInc
< 0 )
637 // As -ve as we can go
638 nScrollInc
= -m_yScrollPosition
;
640 else // check for the other bound
642 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
643 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
645 // As +ve as we can go
646 nScrollInc
= posMax
- m_yScrollPosition
;
654 void wxScrollHelperBase::DoPrepareDC(wxDC
& dc
)
656 wxPoint pt
= dc
.GetDeviceOrigin();
658 // It may actually be correct to always query
659 // the m_sign from the DC here, but I leave the
660 // #ifdef GTK for now.
661 if (m_win
->GetLayoutDirection() == wxLayout_RightToLeft
)
662 dc
.SetDeviceOrigin( pt
.x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
,
663 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
666 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
667 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
668 dc
.SetUserScale( m_scaleX
, m_scaleY
);
671 void wxScrollHelperBase::SetScrollRate( int xstep
, int ystep
)
673 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
674 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
676 m_xScrollPixelsPerLine
= xstep
;
677 m_yScrollPixelsPerLine
= ystep
;
679 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
680 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
682 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
683 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
684 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
689 void wxScrollHelperBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
692 *x_unit
= m_xScrollPixelsPerLine
;
694 *y_unit
= m_yScrollPixelsPerLine
;
698 int wxScrollHelperBase::GetScrollLines( int orient
) const
700 if ( orient
== wxHORIZONTAL
)
701 return m_xScrollLines
;
703 return m_yScrollLines
;
706 int wxScrollHelperBase::GetScrollPageSize(int orient
) const
708 if ( orient
== wxHORIZONTAL
)
709 return m_xScrollLinesPerPage
;
711 return m_yScrollLinesPerPage
;
714 void wxScrollHelperBase::SetScrollPageSize(int orient
, int pageSize
)
716 if ( orient
== wxHORIZONTAL
)
717 m_xScrollLinesPerPage
= pageSize
;
719 m_yScrollLinesPerPage
= pageSize
;
722 void wxScrollHelperBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
724 m_xScrollingEnabled
= x_scroll
;
725 m_yScrollingEnabled
= y_scroll
;
728 // Where the current view starts from
729 void wxScrollHelperBase::DoGetViewStart (int *x
, int *y
) const
732 *x
= m_xScrollPosition
;
734 *y
= m_yScrollPosition
;
737 void wxScrollHelperBase::DoCalcScrolledPosition(int x
, int y
,
738 int *xx
, int *yy
) const
741 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
743 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
746 void wxScrollHelperBase::DoCalcUnscrolledPosition(int x
, int y
,
747 int *xx
, int *yy
) const
750 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
752 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
755 // ----------------------------------------------------------------------------
757 // ----------------------------------------------------------------------------
759 bool wxScrollHelperBase::ScrollLayout()
761 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
763 // If we're the scroll target, take into account the
764 // virtual size and scrolled position of the window.
766 int x
= 0, y
= 0, w
= 0, h
= 0;
767 CalcScrolledPosition(0,0, &x
,&y
);
768 m_win
->GetVirtualSize(&w
, &h
);
769 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
773 // fall back to default for LayoutConstraints
774 return m_win
->wxWindow::Layout();
777 void wxScrollHelperBase::ScrollDoSetVirtualSize(int x
, int y
)
779 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
782 if (m_win
->GetAutoLayout())
786 // wxWindow's GetBestVirtualSize returns the actual window size,
787 // whereas we want to return the virtual size
788 wxSize
wxScrollHelperBase::ScrollGetBestVirtualSize() const
790 wxSize
clientSize(m_win
->GetClientSize());
791 if ( m_win
->GetSizer() )
792 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
797 // ----------------------------------------------------------------------------
799 // ----------------------------------------------------------------------------
801 // Default OnSize resets scrollbars, if any
802 void wxScrollHelperBase::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
804 if ( m_targetWindow
->GetAutoLayout() )
806 wxSize size
= m_targetWindow
->GetBestVirtualSize();
808 // This will call ::Layout() and ::AdjustScrollbars()
809 m_win
->SetVirtualSize( size
);
817 // This calls OnDraw, having adjusted the origin according to the current
819 void wxScrollHelperBase::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
821 // don't use m_targetWindow here, this is always called for ourselves
828 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
829 // compatibility here - if we used OnKeyDown(), the programs which process
830 // arrows themselves in their OnChar() would never get the message and like
831 // this they always have the priority
832 void wxScrollHelperBase::HandleOnChar(wxKeyEvent
& event
)
834 if ( !m_kbdScrollingEnabled
)
840 // prepare the event this key press maps to
841 wxScrollWinEvent newEvent
;
843 newEvent
.SetPosition(0);
844 newEvent
.SetEventObject(m_win
);
845 newEvent
.SetId(m_win
->GetId());
847 // this is the default, it's changed to wxHORIZONTAL below if needed
848 newEvent
.SetOrientation(wxVERTICAL
);
850 // some key events result in scrolling in both horizontal and vertical
851 // direction, e.g. Ctrl-{Home,End}, if this flag is true we should generate
852 // a second event in horizontal direction in addition to the primary one
853 bool sendHorizontalToo
= false;
855 switch ( event
.GetKeyCode() )
858 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
862 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
866 newEvent
.SetEventType(wxEVT_SCROLLWIN_TOP
);
868 sendHorizontalToo
= event
.ControlDown();
872 newEvent
.SetEventType(wxEVT_SCROLLWIN_BOTTOM
);
874 sendHorizontalToo
= event
.ControlDown();
878 newEvent
.SetOrientation(wxHORIZONTAL
);
882 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
886 newEvent
.SetOrientation(wxHORIZONTAL
);
890 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
894 // not a scrolling key
899 m_win
->ProcessWindowEvent(newEvent
);
901 if ( sendHorizontalToo
)
903 newEvent
.SetOrientation(wxHORIZONTAL
);
904 m_win
->ProcessWindowEvent(newEvent
);
908 // ----------------------------------------------------------------------------
909 // autoscroll stuff: these functions deal with sending fake scroll events when
910 // a captured mouse is being held outside the window
911 // ----------------------------------------------------------------------------
913 bool wxScrollHelperBase::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
915 // only send the event if the window is scrollable in this direction
916 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
917 return win
->HasScrollbar(event
.GetOrientation());
920 void wxScrollHelperBase::StopAutoScrolling()
923 wxDELETE(m_timerAutoScroll
);
927 void wxScrollHelperBase::HandleOnMouseEnter(wxMouseEvent
& event
)
934 void wxScrollHelperBase::HandleOnMouseLeave(wxMouseEvent
& event
)
936 // don't prevent the usual processing of the event from taking place
939 // when a captured mouse leave a scrolled window we start generate
940 // scrolling events to allow, for example, extending selection beyond the
941 // visible area in some controls
942 if ( wxWindow::GetCapture() == m_targetWindow
)
944 // where is the mouse leaving?
946 wxPoint pt
= event
.GetPosition();
949 orient
= wxHORIZONTAL
;
957 else // we're lower or to the right of the window
959 wxSize size
= m_targetWindow
->GetClientSize();
962 orient
= wxHORIZONTAL
;
963 pos
= m_xScrollLines
;
965 else if ( pt
.y
> size
.y
)
968 pos
= m_yScrollLines
;
970 else // this should be impossible
972 // but seems to happen sometimes under wxMSW - maybe it's a bug
973 // there but for now just ignore it
975 //wxFAIL_MSG( wxT("can't understand where has mouse gone") );
981 // only start the auto scroll timer if the window can be scrolled in
983 if ( !m_targetWindow
->HasScrollbar(orient
) )
987 delete m_timerAutoScroll
;
988 m_timerAutoScroll
= new wxAutoScrollTimer
990 m_targetWindow
, this,
991 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
992 : wxEVT_SCROLLWIN_LINEDOWN
,
996 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1003 #if wxUSE_MOUSEWHEEL
1005 void wxScrollHelperBase::HandleOnMouseWheel(wxMouseEvent
& event
)
1007 m_wheelRotation
+= event
.GetWheelRotation();
1008 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1009 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1014 wxScrollWinEvent newEvent
;
1016 newEvent
.SetPosition(0);
1017 newEvent
.SetOrientation( event
.GetWheelAxis() == 0 ? wxVERTICAL
: wxHORIZONTAL
);
1018 newEvent
.SetEventObject(m_win
);
1020 if ( event
.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL
)
1023 if (event
.IsPageScroll())
1026 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1028 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1030 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1034 lines
*= event
.GetLinesPerAction();
1036 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1038 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1040 int times
= abs(lines
);
1041 for (; times
> 0; times
--)
1042 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1047 #endif // wxUSE_MOUSEWHEEL
1049 void wxScrollHelperBase::HandleOnChildFocus(wxChildFocusEvent
& event
)
1051 // this event should be processed by all windows in parenthood chain,
1052 // e.g. so that nested wxScrolledWindows work correctly
1055 // find the immediate child under which the window receiving focus is:
1056 wxWindow
*win
= event
.GetWindow();
1058 if ( win
== m_targetWindow
)
1059 return; // nothing to do
1061 #if defined( __WXOSX__ ) && wxUSE_SCROLLBAR
1062 if (wxDynamicCast(win
, wxScrollBar
))
1066 // Fixing ticket: http://trac.wxwidgets.org/ticket/9563
1067 // When a child inside a wxControlContainer receives a focus, the
1068 // wxControlContainer generates an artificial wxChildFocusEvent for
1069 // itself, telling its parent that 'it' received the focus. The effect is
1070 // that this->HandleOnChildFocus is called twice, first with the
1071 // artificial wxChildFocusEvent and then with the original event. We need
1072 // to ignore the artificial event here or otherwise HandleOnChildFocus
1073 // would first scroll the target window to make the entire
1074 // wxControlContainer visible and immediately afterwards scroll the target
1075 // window again to make the child widget visible. This leads to ugly
1076 // flickering when using nested wxPanels/wxScrolledWindows.
1078 // Ignore this event if 'win' is derived from wxControlContainer AND its
1079 // parent is the m_targetWindow AND 'win' is not actually reciving the
1080 // focus (win != FindFocus). TODO: This affects all wxControlContainer
1081 // objects, but wxControlContainer is not part of the wxWidgets RTTI and
1082 // so wxDynamicCast(win, wxControlContainer) does not compile. Find a way
1083 // to determine if 'win' derives from wxControlContainer. Until then,
1084 // testing if 'win' derives from wxPanel will probably get >90% of all
1087 wxWindow
*actual_focus
=wxWindow::FindFocus();
1088 if (win
!= actual_focus
&&
1089 wxDynamicCast(win
, wxPanel
) != 0 &&
1090 win
->GetParent() == m_targetWindow
)
1091 // if win is a wxPanel and receives the focus, it should not be
1092 // scrolled into view
1095 const wxRect
viewRect(m_targetWindow
->GetClientRect());
1097 // For composite controls such as wxComboCtrl we should try to fit the
1098 // entire control inside the visible area of the target window, not just
1099 // the focused child of the control. Otherwise we'd make only the textctrl
1100 // part of a wxComboCtrl visible and the button would still be outside the
1101 // scrolled area. But do so only if the parent fits *entirely* inside the
1102 // scrolled window. In other situations, such as nested wxPanel or
1103 // wxScrolledWindows, the parent might be way too big to fit inside the
1104 // scrolled window. If that is the case, then make only the focused window
1106 if ( win
->GetParent() != m_targetWindow
)
1108 wxWindow
*parent
=win
->GetParent();
1109 wxSize parent_size
=parent
->GetSize();
1110 if (parent_size
.GetWidth() <= viewRect
.GetWidth() &&
1111 parent_size
.GetHeight() <= viewRect
.GetHeight())
1112 // make the immediate parent visible instead of the focused control
1116 // make win position relative to the m_targetWindow viewing area instead of
1119 winRect(m_targetWindow
->ScreenToClient(win
->GetScreenPosition()),
1122 // check if it's fully visible
1123 if ( viewRect
.Contains(winRect
) )
1125 // it is, nothing to do
1129 // check if we can make it fully visible: this is only possible if it's not
1130 // larger than our view area
1131 if ( winRect
.GetWidth() > viewRect
.GetWidth() ||
1132 winRect
.GetHeight() > viewRect
.GetHeight() )
1134 // we can't make it fit so avoid scrolling it at all, this is only
1135 // going to be confusing and not helpful
1140 // do make the window fit inside the view area by scrolling to it
1142 GetScrollPixelsPerUnit(&stepx
, &stepy
);
1145 GetViewStart(&startx
, &starty
);
1147 // first in vertical direction:
1152 if ( winRect
.GetTop() < 0 )
1154 diff
= winRect
.GetTop();
1156 else if ( winRect
.GetBottom() > viewRect
.GetHeight() )
1158 diff
= winRect
.GetBottom() - viewRect
.GetHeight() + 1;
1159 // round up to next scroll step if we can't get exact position,
1160 // so that the window is fully visible:
1164 starty
= (starty
* stepy
+ diff
) / stepy
;
1172 if ( winRect
.GetLeft() < 0 )
1174 diff
= winRect
.GetLeft();
1176 else if ( winRect
.GetRight() > viewRect
.GetWidth() )
1178 diff
= winRect
.GetRight() - viewRect
.GetWidth() + 1;
1179 // round up to next scroll step if we can't get exact position,
1180 // so that the window is fully visible:
1184 startx
= (startx
* stepx
+ diff
) / stepx
;
1187 Scroll(startx
, starty
);
1191 #ifdef wxHAS_GENERIC_SCROLLWIN
1193 // ----------------------------------------------------------------------------
1194 // wxScrollHelper implementation
1195 // ----------------------------------------------------------------------------
1197 wxScrollHelper::wxScrollHelper(wxWindow
*winToScroll
)
1198 : wxScrollHelperBase(winToScroll
)
1201 m_yVisibility
= wxSHOW_SB_DEFAULT
;
1204 void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz
,
1205 wxScrollbarVisibility vert
)
1207 if ( horz
!= m_xVisibility
|| vert
!= m_yVisibility
)
1209 m_xVisibility
= horz
;
1210 m_yVisibility
= vert
;
1217 wxScrollHelper::DoAdjustScrollbar(int orient
,
1222 int& scrollPosition
,
1223 int& scrollLinesPerPage
,
1224 wxScrollbarVisibility visibility
)
1226 // scroll lines per page: if 0, no scrolling is needed
1227 // check if we need scrollbar in this direction at all
1228 if ( pixelsPerUnit
== 0 || clientSize
>= virtSize
)
1230 // scrolling is disabled or unnecessary
1233 scrollLinesPerPage
= 0;
1235 else // might need scrolling
1237 // Round up integer division to catch any "leftover" client space.
1238 scrollUnits
= (virtSize
+ pixelsPerUnit
- 1) / pixelsPerUnit
;
1240 // Calculate the number of fully scroll units
1241 scrollLinesPerPage
= clientSize
/ pixelsPerUnit
;
1243 if ( scrollLinesPerPage
>= scrollUnits
)
1245 // we're big enough to not need scrolling
1248 scrollLinesPerPage
= 0;
1250 else // we do need a scrollbar
1252 if ( scrollLinesPerPage
< 1 )
1253 scrollLinesPerPage
= 1;
1255 // Correct position if greater than extent of canvas minus
1256 // the visible portion of it or if below zero
1257 const int posMax
= scrollUnits
- scrollLinesPerPage
;
1258 if ( scrollPosition
> posMax
)
1259 scrollPosition
= posMax
;
1260 else if ( scrollPosition
< 0 )
1265 // in wxSHOW_SB_NEVER case don't show the scrollbar even if it's needed, in
1266 // wxSHOW_SB_ALWAYS case show the scrollbar even if it's not needed by
1267 // passing a special range value to SetScrollbar()
1269 switch ( visibility
)
1271 case wxSHOW_SB_NEVER
:
1275 case wxSHOW_SB_ALWAYS
:
1276 range
= scrollUnits
? scrollUnits
: -1;
1280 wxFAIL_MSG( wxS("unknown scrollbar visibility") );
1283 case wxSHOW_SB_DEFAULT
:
1284 range
= scrollUnits
;
1289 m_win
->SetScrollbar(orient
, scrollPosition
, scrollLinesPerPage
, range
);
1292 void wxScrollHelper::AdjustScrollbars()
1294 static wxRecursionGuardFlag s_flagReentrancy
;
1295 wxRecursionGuard
guard(s_flagReentrancy
);
1296 if ( guard
.IsInside() )
1298 // don't reenter AdjustScrollbars() while another call to
1299 // AdjustScrollbars() is in progress because this may lead to calling
1300 // ScrollWindow() twice and this can really happen under MSW if
1301 // SetScrollbar() call below adds or removes the scrollbar which
1302 // changes the window size and hence results in another
1303 // AdjustScrollbars() call
1307 int oldXScroll
= m_xScrollPosition
;
1308 int oldYScroll
= m_yScrollPosition
;
1310 // we may need to readjust the scrollbars several times as enabling one of
1311 // them reduces the area available for the window contents and so can make
1312 // the other scrollbar necessary now although it wasn't necessary before
1314 // VZ: normally this loop should be over in at most 2 iterations, I don't
1315 // know why do we need 5 of them
1316 for ( int iterationCount
= 0; iterationCount
< 5; iterationCount
++ )
1318 wxSize clientSize
= GetTargetSize();
1319 const wxSize virtSize
= m_targetWindow
->GetVirtualSize();
1321 // this block of code tries to work around the following problem: the
1322 // window could have been just resized to have enough space to show its
1323 // full contents without the scrollbars, but its client size could be
1324 // not big enough because it does have the scrollbars right now and so
1325 // the scrollbars would remain even though we don't need them any more
1327 // to prevent this from happening, check if we have enough space for
1328 // everything without the scrollbars and explicitly disable them then
1329 const wxSize availSize
= GetSizeAvailableForScrollTarget(
1330 m_win
->GetSize() - m_win
->GetWindowBorderSize());
1331 if ( availSize
!= clientSize
)
1333 if ( availSize
.x
>= virtSize
.x
&& availSize
.y
>= virtSize
.y
)
1335 // this will be enough to make the scrollbars disappear below
1336 // and then the client size will indeed become equal to the
1337 // full available size
1338 clientSize
= availSize
;
1343 DoAdjustScrollbar(wxHORIZONTAL
,
1346 m_xScrollPixelsPerLine
,
1349 m_xScrollLinesPerPage
,
1352 DoAdjustScrollbar(wxVERTICAL
,
1355 m_yScrollPixelsPerLine
,
1358 m_yScrollLinesPerPage
,
1362 // If a scrollbar (dis)appeared as a result of this, we need to adjust
1363 // them again but if the client size didn't change, then we're done
1364 if ( GetTargetSize() == clientSize
)
1369 // Sorry, some Motif-specific code to implement a backing pixmap
1370 // for the wxRETAINED style. Implementing a backing store can't
1371 // be entirely generic because it relies on the wxWindowDC implementation
1372 // to duplicate X drawing calls for the backing pixmap.
1374 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
1376 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
1378 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
1379 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
1380 if (m_targetWindow
->GetBackingPixmap() &&
1381 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
1382 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
1384 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
1385 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
1388 if (!m_targetWindow
->GetBackingPixmap() &&
1389 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
1391 int depth
= wxDisplayDepth();
1392 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
1393 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
1394 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
1395 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
1401 if (oldXScroll
!= m_xScrollPosition
)
1403 if (m_xScrollingEnabled
)
1404 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
1407 m_targetWindow
->Refresh(true, GetScrollRect());
1410 if (oldYScroll
!= m_yScrollPosition
)
1412 if (m_yScrollingEnabled
)
1413 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
1416 m_targetWindow
->Refresh(true, GetScrollRect());
1420 void wxScrollHelper::DoScroll( int x_pos
, int y_pos
)
1422 if (!m_targetWindow
)
1425 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
1426 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
1429 GetTargetSize(&w
, &h
);
1431 // compute new position:
1432 int new_x
= m_xScrollPosition
;
1433 int new_y
= m_yScrollPosition
;
1435 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
1439 // Calculate page size i.e. number of scroll units you get on the
1440 // current client window
1441 int noPagePositions
= w
/m_xScrollPixelsPerLine
;
1442 if (noPagePositions
< 1) noPagePositions
= 1;
1444 // Correct position if greater than extent of canvas minus
1445 // the visible portion of it or if below zero
1446 new_x
= wxMin( m_xScrollLines
-noPagePositions
, new_x
);
1447 new_x
= wxMax( 0, new_x
);
1449 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
1453 // Calculate page size i.e. number of scroll units you get on the
1454 // current client window
1455 int noPagePositions
= h
/m_yScrollPixelsPerLine
;
1456 if (noPagePositions
< 1) noPagePositions
= 1;
1458 // Correct position if greater than extent of canvas minus
1459 // the visible portion of it or if below zero
1460 new_y
= wxMin( m_yScrollLines
-noPagePositions
, new_y
);
1461 new_y
= wxMax( 0, new_y
);
1464 if ( new_x
== m_xScrollPosition
&& new_y
== m_yScrollPosition
)
1465 return; // nothing to do, the position didn't change
1467 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
1468 // otherwise invalidated area could be updated incorrectly later when
1469 // ScrollWindow() makes sure they're repainted before scrolling them
1470 m_targetWindow
->Update();
1472 // update the position and scroll the window now:
1473 if (m_xScrollPosition
!= new_x
)
1475 int old_x
= m_xScrollPosition
;
1476 m_xScrollPosition
= new_x
;
1477 m_win
->SetScrollPos( wxHORIZONTAL
, new_x
);
1478 m_targetWindow
->ScrollWindow( (old_x
-new_x
)*m_xScrollPixelsPerLine
, 0,
1482 if (m_yScrollPosition
!= new_y
)
1484 int old_y
= m_yScrollPosition
;
1485 m_yScrollPosition
= new_y
;
1486 m_win
->SetScrollPos( wxVERTICAL
, new_y
);
1487 m_targetWindow
->ScrollWindow( 0, (old_y
-new_y
)*m_yScrollPixelsPerLine
,
1492 #endif // wxHAS_GENERIC_SCROLLWIN
1494 // ----------------------------------------------------------------------------
1495 // wxScrolled<T> and wxScrolledWindow implementation
1496 // ----------------------------------------------------------------------------
1498 wxSize
wxScrolledT_Helper::FilterBestSize(const wxWindow
*win
,
1499 const wxScrollHelper
*helper
,
1500 const wxSize
& origBest
)
1502 // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1503 // all scrollable windows should behave like this, only those that
1504 // contain children controls within scrollable area
1505 // (i.e., wxScrolledWindow) and other some scrollable windows may
1506 // have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1508 wxSize best
= origBest
;
1510 if ( win
->GetAutoLayout() )
1512 // Only use the content to set the window size in the direction
1513 // where there's no scrolling; otherwise we're going to get a huge
1514 // window in the direction in which scrolling is enabled
1516 helper
->GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1518 // NB: This code used to use *current* size if min size wasn't
1519 // specified, presumably to get some reasonable (i.e., larger than
1520 // minimal) size. But that's a wrong thing to do in GetBestSize(),
1521 // so we use minimal size as specified. If the app needs some
1522 // minimal size for its scrolled window, it should set it and put
1523 // the window into sizer as expandable so that it can use all space
1526 // See also http://svn.wxwidgets.org/viewvc/wx?view=rev&revision=45864
1528 wxSize minSize
= win
->GetMinSize();
1531 best
.x
= minSize
.x
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1534 best
.y
= minSize
.y
+ wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
1541 WXLRESULT
wxScrolledT_Helper::FilterMSWWindowProc(WXUINT nMsg
, WXLRESULT rc
)
1544 // we need to process arrows ourselves for scrolling
1545 if ( nMsg
== WM_GETDLGCODE
)
1547 rc
|= DLGC_WANTARROWS
;
1554 // NB: skipping wxScrolled<T> in wxRTTI information because being a templte,
1555 // it doesn't and can't implement wxRTTI support
1556 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)