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 // Notice that this method is currently duplicated in the method with the same
190 // name in wxVarScrollHelperEvtHandler class, until this is fixed, the other
191 // copy of the method needs to be modified every time this version is.
192 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
194 wxEventType evType
= event
.GetEventType();
196 // Pass it on to the real handler: notice that we must not call
197 // ProcessEvent() on this object itself as it wouldn't pass it to the next
198 // handler (i.e. the real window) if we're called from a previous handler
199 // (as indicated by "process here only" flag being set) and we do want to
200 // execute the handler defined in the window we're associated with right
201 // now, without waiting until TryAfter() is called from wxEvtHandler.
202 bool processed
= m_nextHandler
->ProcessEvent(event
);
204 // always process the size events ourselves, even if the user code handles
205 // them as well, as we need to AdjustScrollbars()
207 // NB: it is important to do it after processing the event in the normal
208 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
209 // scrollbar[s] (dis)appear and it should be seen by the user code
211 if ( evType
== wxEVT_SIZE
)
213 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
217 if ( processed
&& event
.IsCommandEvent())
220 // For wxEVT_PAINT the user code can either handle this event as usual or
221 // override virtual OnDraw(), so if the event hasn't been handled we need
222 // to call this virtual function ourselves.
224 #ifndef __WXUNIVERSAL__
225 // in wxUniversal "processed" will always be true, because
226 // all windows use the paint event to draw themselves.
227 // In this case we can't use this flag to determine if a custom
228 // paint event handler already drew our window and we just
229 // call OnDraw() anyway.
231 #endif // !__WXUNIVERSAL__
232 evType
== wxEVT_PAINT
)
234 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
238 if ( evType
== wxEVT_CHILD_FOCUS
)
240 m_scrollHelper
->HandleOnChildFocus((wxChildFocusEvent
&)event
);
244 // reset the skipped flag (which might have been set to true in
245 // ProcessEvent() above) to be able to test it below
246 bool wasSkipped
= event
.GetSkipped();
250 if ( evType
== wxEVT_SCROLLWIN_TOP
||
251 evType
== wxEVT_SCROLLWIN_BOTTOM
||
252 evType
== wxEVT_SCROLLWIN_LINEUP
||
253 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
254 evType
== wxEVT_SCROLLWIN_PAGEUP
||
255 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
256 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
257 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
259 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
260 if ( !event
.GetSkipped() )
262 // it makes sense to indicate that we processed the message as we
263 // did scroll the window (and also notice that wxAutoScrollTimer
264 // relies on our return value to stop scrolling when we are at top
265 // or bottom already)
271 if ( evType
== wxEVT_ENTER_WINDOW
)
273 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
275 else if ( evType
== wxEVT_LEAVE_WINDOW
)
277 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
280 // Use GTK's own scroll wheel handling in GtkScrolledWindow
282 else if ( evType
== wxEVT_MOUSEWHEEL
)
284 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
288 #endif // wxUSE_MOUSEWHEEL
289 else if ( evType
== wxEVT_CHAR
)
291 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
292 if ( !event
.GetSkipped() )
299 event
.Skip(wasSkipped
);
301 // We called ProcessEvent() on the next handler, meaning that we explicitly
302 // worked around the request to process the event in this handler only. As
303 // explained above, this is unfortunately really necessary but the trouble
304 // is that the event will continue to be post-processed by the previous
305 // handler resulting in duplicate calls to event handlers. Call the special
306 // function below to prevent this from happening, base class DoTryChain()
307 // will check for it and behave accordingly.
309 // And if we're not called from DoTryChain(), this won't do anything anyhow.
310 event
.DidntHonourProcessOnlyIn();
315 // ============================================================================
316 // wxAnyScrollHelperBase and wxScrollHelperBase implementation
317 // ============================================================================
319 // ----------------------------------------------------------------------------
320 // wxAnyScrollHelperBase
321 // ----------------------------------------------------------------------------
323 wxAnyScrollHelperBase::wxAnyScrollHelperBase(wxWindow
* win
)
325 wxASSERT_MSG( win
, wxT("associated window can't be NULL in wxScrollHelper") );
328 m_targetWindow
= NULL
;
330 m_kbdScrollingEnabled
= true;
333 // ----------------------------------------------------------------------------
334 // wxScrollHelperBase construction
335 // ----------------------------------------------------------------------------
337 wxScrollHelperBase::wxScrollHelperBase(wxWindow
*win
)
338 : wxAnyScrollHelperBase(win
)
340 m_xScrollPixelsPerLine
=
341 m_yScrollPixelsPerLine
=
346 m_xScrollLinesPerPage
=
347 m_yScrollLinesPerPage
= 0;
349 m_xScrollingEnabled
=
350 m_yScrollingEnabled
= true;
358 m_timerAutoScroll
= NULL
;
362 m_win
->SetScrollHelper(static_cast<wxScrollHelper
*>(this));
364 // by default, the associated window is also the target window
365 DoSetTargetWindow(win
);
368 wxScrollHelperBase::~wxScrollHelperBase()
375 // ----------------------------------------------------------------------------
376 // setting scrolling parameters
377 // ----------------------------------------------------------------------------
379 void wxScrollHelperBase::SetScrollbars(int pixelsPerUnitX
,
387 // Convert positions expressed in scroll units to positions in pixels.
388 int xPosInPixels
= (xPos
+ m_xScrollPosition
)*m_xScrollPixelsPerLine
,
389 yPosInPixels
= (yPos
+ m_yScrollPosition
)*m_yScrollPixelsPerLine
;
393 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
394 (noUnitsX
< m_xScrollLines
&& xPosInPixels
> pixelsPerUnitX
* noUnitsX
) ||
396 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
397 (noUnitsY
< m_yScrollLines
&& yPosInPixels
> pixelsPerUnitY
* noUnitsY
) ||
398 (xPos
!= m_xScrollPosition
) ||
399 (yPos
!= m_yScrollPosition
)
402 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
403 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
404 m_xScrollPosition
= xPos
;
405 m_yScrollPosition
= yPos
;
407 int w
= noUnitsX
* pixelsPerUnitX
;
408 int h
= noUnitsY
* pixelsPerUnitY
;
410 // For better backward compatibility we set persisting limits
411 // here not just the size. It makes SetScrollbars 'sticky'
412 // emulating the old non-autoscroll behaviour.
413 // m_targetWindow->SetVirtualSizeHints( w, h );
415 // The above should arguably be deprecated, this however we still need.
417 // take care not to set 0 virtual size, 0 means that we don't have any
418 // scrollbars and hence we should use the real size instead of the virtual
419 // one which is indicated by using wxDefaultCoord
420 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
421 h
? h
: wxDefaultCoord
);
423 if (do_refresh
&& !noRefresh
)
424 m_targetWindow
->Refresh(true, GetScrollRect());
426 #ifndef __WXUNIVERSAL__
427 // If the target is not the same as the window with the scrollbars,
428 // then we need to update the scrollbars here, since they won't have
429 // been updated by SetVirtualSize().
430 if ( m_targetWindow
!= m_win
)
431 #endif // !__WXUNIVERSAL__
435 #ifndef __WXUNIVERSAL__
438 // otherwise this has been done by AdjustScrollbars, above
440 #endif // !__WXUNIVERSAL__
443 // ----------------------------------------------------------------------------
444 // [target] window handling
445 // ----------------------------------------------------------------------------
447 void wxScrollHelperBase::DeleteEvtHandler()
449 // search for m_handler in the handler list
450 if ( m_win
&& m_handler
)
452 if ( m_win
->RemoveEventHandler(m_handler
) )
456 //else: something is very wrong, so better [maybe] leak memory than
457 // risk a crash because of double deletion
463 void wxScrollHelperBase::DoSetTargetWindow(wxWindow
*target
)
465 m_targetWindow
= target
;
467 target
->MacSetClipChildren( true ) ;
470 // install the event handler which will intercept the events we're
471 // interested in (but only do it for our real window, not the target window
472 // which we scroll - we don't need to hijack its events)
473 if ( m_targetWindow
== m_win
)
475 // if we already have a handler, delete it first
478 m_handler
= new wxScrollHelperEvtHandler(this);
479 m_targetWindow
->PushEventHandler(m_handler
);
483 void wxScrollHelperBase::SetTargetWindow(wxWindow
*target
)
485 wxCHECK_RET( target
, wxT("target window must not be NULL") );
487 if ( target
== m_targetWindow
)
490 DoSetTargetWindow(target
);
493 // ----------------------------------------------------------------------------
494 // scrolling implementation itself
495 // ----------------------------------------------------------------------------
497 void wxScrollHelperBase::HandleOnScroll(wxScrollWinEvent
& event
)
499 int nScrollInc
= CalcScrollInc(event
);
500 if ( nScrollInc
== 0 )
502 // can't scroll further
508 bool needsRefresh
= false;
511 int orient
= event
.GetOrientation();
512 if (orient
== wxHORIZONTAL
)
514 if ( m_xScrollingEnabled
)
516 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
525 if ( m_yScrollingEnabled
)
527 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
537 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
538 // otherwise invalidated area could be updated incorrectly later when
539 // ScrollWindow() makes sure they're repainted before scrolling them
541 // wxWindowMac is taking care of making sure the update area is correctly
542 // set up, while not forcing an immediate redraw
544 m_targetWindow
->Update();
548 if (orient
== wxHORIZONTAL
)
550 m_xScrollPosition
+= nScrollInc
;
551 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
555 m_yScrollPosition
+= nScrollInc
;
556 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
561 m_targetWindow
->Refresh(true, GetScrollRect());
565 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
569 int wxScrollHelperBase::CalcScrollInc(wxScrollWinEvent
& event
)
571 int pos
= event
.GetPosition();
572 int orient
= event
.GetOrientation();
575 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
577 if (orient
== wxHORIZONTAL
)
578 nScrollInc
= - m_xScrollPosition
;
580 nScrollInc
= - m_yScrollPosition
;
582 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
584 if (orient
== wxHORIZONTAL
)
585 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
587 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
589 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
593 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
597 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
599 if (orient
== wxHORIZONTAL
)
600 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
602 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
604 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
606 if (orient
== wxHORIZONTAL
)
607 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
609 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
611 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
612 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
614 if (orient
== wxHORIZONTAL
)
615 nScrollInc
= pos
- m_xScrollPosition
;
617 nScrollInc
= pos
- m_yScrollPosition
;
620 if (orient
== wxHORIZONTAL
)
622 if ( m_xScrollPosition
+ nScrollInc
< 0 )
624 // As -ve as we can go
625 nScrollInc
= -m_xScrollPosition
;
627 else // check for the other bound
629 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
630 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
632 // As +ve as we can go
633 nScrollInc
= posMax
- m_xScrollPosition
;
639 if ( m_yScrollPosition
+ nScrollInc
< 0 )
641 // As -ve as we can go
642 nScrollInc
= -m_yScrollPosition
;
644 else // check for the other bound
646 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
647 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
649 // As +ve as we can go
650 nScrollInc
= posMax
- m_yScrollPosition
;
658 void wxScrollHelperBase::DoPrepareDC(wxDC
& dc
)
660 wxPoint pt
= dc
.GetDeviceOrigin();
662 // It may actually be correct to always query
663 // the m_sign from the DC here, but I leave the
664 // #ifdef GTK for now.
665 if (m_win
->GetLayoutDirection() == wxLayout_RightToLeft
)
666 dc
.SetDeviceOrigin( pt
.x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
,
667 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
670 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
671 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
672 dc
.SetUserScale( m_scaleX
, m_scaleY
);
675 void wxScrollHelperBase::SetScrollRate( int xstep
, int ystep
)
677 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
678 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
680 m_xScrollPixelsPerLine
= xstep
;
681 m_yScrollPixelsPerLine
= ystep
;
683 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
684 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
686 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
687 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
688 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
693 void wxScrollHelperBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
696 *x_unit
= m_xScrollPixelsPerLine
;
698 *y_unit
= m_yScrollPixelsPerLine
;
702 int wxScrollHelperBase::GetScrollLines( int orient
) const
704 if ( orient
== wxHORIZONTAL
)
705 return m_xScrollLines
;
707 return m_yScrollLines
;
710 int wxScrollHelperBase::GetScrollPageSize(int orient
) const
712 if ( orient
== wxHORIZONTAL
)
713 return m_xScrollLinesPerPage
;
715 return m_yScrollLinesPerPage
;
718 void wxScrollHelperBase::SetScrollPageSize(int orient
, int pageSize
)
720 if ( orient
== wxHORIZONTAL
)
721 m_xScrollLinesPerPage
= pageSize
;
723 m_yScrollLinesPerPage
= pageSize
;
726 void wxScrollHelperBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
728 m_xScrollingEnabled
= x_scroll
;
729 m_yScrollingEnabled
= y_scroll
;
732 // Where the current view starts from
733 void wxScrollHelperBase::DoGetViewStart (int *x
, int *y
) const
736 *x
= m_xScrollPosition
;
738 *y
= m_yScrollPosition
;
741 void wxScrollHelperBase::DoCalcScrolledPosition(int x
, int y
,
742 int *xx
, int *yy
) const
745 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
747 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
750 void wxScrollHelperBase::DoCalcUnscrolledPosition(int x
, int y
,
751 int *xx
, int *yy
) const
754 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
756 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
759 // ----------------------------------------------------------------------------
761 // ----------------------------------------------------------------------------
763 bool wxScrollHelperBase::ScrollLayout()
765 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
767 // If we're the scroll target, take into account the
768 // virtual size and scrolled position of the window.
770 int x
= 0, y
= 0, w
= 0, h
= 0;
771 CalcScrolledPosition(0,0, &x
,&y
);
772 m_win
->GetVirtualSize(&w
, &h
);
773 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
777 // fall back to default for LayoutConstraints
778 return m_win
->wxWindow::Layout();
781 void wxScrollHelperBase::ScrollDoSetVirtualSize(int x
, int y
)
783 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
786 if (m_win
->GetAutoLayout())
790 // wxWindow's GetBestVirtualSize returns the actual window size,
791 // whereas we want to return the virtual size
792 wxSize
wxScrollHelperBase::ScrollGetBestVirtualSize() const
794 wxSize
clientSize(m_win
->GetClientSize());
795 if ( m_win
->GetSizer() )
796 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
801 // ----------------------------------------------------------------------------
803 // ----------------------------------------------------------------------------
805 // Default OnSize resets scrollbars, if any
806 void wxScrollHelperBase::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
808 if ( m_targetWindow
->GetAutoLayout() )
810 wxSize size
= m_targetWindow
->GetBestVirtualSize();
812 // This will call ::Layout() and ::AdjustScrollbars()
813 m_win
->SetVirtualSize( size
);
821 // This calls OnDraw, having adjusted the origin according to the current
823 void wxAnyScrollHelperBase::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
825 // don't use m_targetWindow here, this is always called for ourselves
832 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
833 // compatibility here - if we used OnKeyDown(), the programs which process
834 // arrows themselves in their OnChar() would never get the message and like
835 // this they always have the priority
836 void wxAnyScrollHelperBase::HandleOnChar(wxKeyEvent
& event
)
838 if ( !m_kbdScrollingEnabled
)
844 // prepare the event this key press maps to
845 wxScrollWinEvent newEvent
;
847 newEvent
.SetPosition(0);
848 newEvent
.SetEventObject(m_win
);
849 newEvent
.SetId(m_win
->GetId());
851 // this is the default, it's changed to wxHORIZONTAL below if needed
852 newEvent
.SetOrientation(wxVERTICAL
);
854 // some key events result in scrolling in both horizontal and vertical
855 // direction, e.g. Ctrl-{Home,End}, if this flag is true we should generate
856 // a second event in horizontal direction in addition to the primary one
857 bool sendHorizontalToo
= false;
859 switch ( event
.GetKeyCode() )
862 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
866 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
870 newEvent
.SetEventType(wxEVT_SCROLLWIN_TOP
);
872 sendHorizontalToo
= event
.ControlDown();
876 newEvent
.SetEventType(wxEVT_SCROLLWIN_BOTTOM
);
878 sendHorizontalToo
= event
.ControlDown();
882 newEvent
.SetOrientation(wxHORIZONTAL
);
886 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
890 newEvent
.SetOrientation(wxHORIZONTAL
);
894 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
898 // not a scrolling key
903 m_win
->ProcessWindowEvent(newEvent
);
905 if ( sendHorizontalToo
)
907 newEvent
.SetOrientation(wxHORIZONTAL
);
908 m_win
->ProcessWindowEvent(newEvent
);
912 // ----------------------------------------------------------------------------
913 // autoscroll stuff: these functions deal with sending fake scroll events when
914 // a captured mouse is being held outside the window
915 // ----------------------------------------------------------------------------
917 bool wxScrollHelperBase::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
919 // only send the event if the window is scrollable in this direction
920 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
921 return win
->HasScrollbar(event
.GetOrientation());
924 void wxScrollHelperBase::StopAutoScrolling()
927 wxDELETE(m_timerAutoScroll
);
931 void wxScrollHelperBase::HandleOnMouseEnter(wxMouseEvent
& event
)
938 void wxScrollHelperBase::HandleOnMouseLeave(wxMouseEvent
& event
)
940 // don't prevent the usual processing of the event from taking place
943 // when a captured mouse leave a scrolled window we start generate
944 // scrolling events to allow, for example, extending selection beyond the
945 // visible area in some controls
946 if ( wxWindow::GetCapture() == m_targetWindow
)
948 // where is the mouse leaving?
950 wxPoint pt
= event
.GetPosition();
953 orient
= wxHORIZONTAL
;
961 else // we're lower or to the right of the window
963 wxSize size
= m_targetWindow
->GetClientSize();
966 orient
= wxHORIZONTAL
;
967 pos
= m_xScrollLines
;
969 else if ( pt
.y
> size
.y
)
972 pos
= m_yScrollLines
;
974 else // this should be impossible
976 // but seems to happen sometimes under wxMSW - maybe it's a bug
977 // there but for now just ignore it
979 //wxFAIL_MSG( wxT("can't understand where has mouse gone") );
985 // only start the auto scroll timer if the window can be scrolled in
987 if ( !m_targetWindow
->HasScrollbar(orient
) )
991 delete m_timerAutoScroll
;
992 m_timerAutoScroll
= new wxAutoScrollTimer
994 m_targetWindow
, this,
995 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
996 : wxEVT_SCROLLWIN_LINEDOWN
,
1000 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1007 #if wxUSE_MOUSEWHEEL
1009 void wxScrollHelperBase::HandleOnMouseWheel(wxMouseEvent
& event
)
1011 m_wheelRotation
+= event
.GetWheelRotation();
1012 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1013 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1018 wxScrollWinEvent newEvent
;
1020 newEvent
.SetPosition(0);
1021 newEvent
.SetOrientation( event
.GetWheelAxis() == 0 ? wxVERTICAL
: wxHORIZONTAL
);
1022 newEvent
.SetEventObject(m_win
);
1024 if ( event
.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL
)
1027 if (event
.IsPageScroll())
1030 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1032 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1034 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1038 lines
*= event
.GetLinesPerAction();
1040 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1042 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1044 int times
= abs(lines
);
1045 for (; times
> 0; times
--)
1046 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1051 #endif // wxUSE_MOUSEWHEEL
1053 void wxScrollHelperBase::HandleOnChildFocus(wxChildFocusEvent
& event
)
1055 // this event should be processed by all windows in parenthood chain,
1056 // e.g. so that nested wxScrolledWindows work correctly
1059 // find the immediate child under which the window receiving focus is:
1060 wxWindow
*win
= event
.GetWindow();
1062 if ( win
== m_targetWindow
)
1063 return; // nothing to do
1065 #if defined( __WXOSX__ ) && wxUSE_SCROLLBAR
1066 if (wxDynamicCast(win
, wxScrollBar
))
1070 // Fixing ticket: http://trac.wxwidgets.org/ticket/9563
1071 // When a child inside a wxControlContainer receives a focus, the
1072 // wxControlContainer generates an artificial wxChildFocusEvent for
1073 // itself, telling its parent that 'it' received the focus. The effect is
1074 // that this->HandleOnChildFocus is called twice, first with the
1075 // artificial wxChildFocusEvent and then with the original event. We need
1076 // to ignore the artificial event here or otherwise HandleOnChildFocus
1077 // would first scroll the target window to make the entire
1078 // wxControlContainer visible and immediately afterwards scroll the target
1079 // window again to make the child widget visible. This leads to ugly
1080 // flickering when using nested wxPanels/wxScrolledWindows.
1082 // Ignore this event if 'win' is derived from wxControlContainer AND its
1083 // parent is the m_targetWindow AND 'win' is not actually reciving the
1084 // focus (win != FindFocus). TODO: This affects all wxControlContainer
1085 // objects, but wxControlContainer is not part of the wxWidgets RTTI and
1086 // so wxDynamicCast(win, wxControlContainer) does not compile. Find a way
1087 // to determine if 'win' derives from wxControlContainer. Until then,
1088 // testing if 'win' derives from wxPanel will probably get >90% of all
1091 wxWindow
*actual_focus
=wxWindow::FindFocus();
1092 if (win
!= actual_focus
&&
1093 wxDynamicCast(win
, wxPanel
) != 0 &&
1094 win
->GetParent() == m_targetWindow
)
1095 // if win is a wxPanel and receives the focus, it should not be
1096 // scrolled into view
1099 const wxRect
viewRect(m_targetWindow
->GetClientRect());
1101 // For composite controls such as wxComboCtrl we should try to fit the
1102 // entire control inside the visible area of the target window, not just
1103 // the focused child of the control. Otherwise we'd make only the textctrl
1104 // part of a wxComboCtrl visible and the button would still be outside the
1105 // scrolled area. But do so only if the parent fits *entirely* inside the
1106 // scrolled window. In other situations, such as nested wxPanel or
1107 // wxScrolledWindows, the parent might be way too big to fit inside the
1108 // scrolled window. If that is the case, then make only the focused window
1110 if ( win
->GetParent() != m_targetWindow
)
1112 wxWindow
*parent
=win
->GetParent();
1113 wxSize parent_size
=parent
->GetSize();
1114 if (parent_size
.GetWidth() <= viewRect
.GetWidth() &&
1115 parent_size
.GetHeight() <= viewRect
.GetHeight())
1116 // make the immediate parent visible instead of the focused control
1120 // make win position relative to the m_targetWindow viewing area instead of
1123 winRect(m_targetWindow
->ScreenToClient(win
->GetScreenPosition()),
1126 // check if it's fully visible
1127 if ( viewRect
.Contains(winRect
) )
1129 // it is, nothing to do
1133 // check if we can make it fully visible: this is only possible if it's not
1134 // larger than our view area
1135 if ( winRect
.GetWidth() > viewRect
.GetWidth() ||
1136 winRect
.GetHeight() > viewRect
.GetHeight() )
1138 // we can't make it fit so avoid scrolling it at all, this is only
1139 // going to be confusing and not helpful
1144 // do make the window fit inside the view area by scrolling to it
1146 GetScrollPixelsPerUnit(&stepx
, &stepy
);
1149 GetViewStart(&startx
, &starty
);
1151 // first in vertical direction:
1156 if ( winRect
.GetTop() < 0 )
1158 diff
= winRect
.GetTop();
1160 else if ( winRect
.GetBottom() > viewRect
.GetHeight() )
1162 diff
= winRect
.GetBottom() - viewRect
.GetHeight() + 1;
1163 // round up to next scroll step if we can't get exact position,
1164 // so that the window is fully visible:
1168 starty
= (starty
* stepy
+ diff
) / stepy
;
1176 if ( winRect
.GetLeft() < 0 )
1178 diff
= winRect
.GetLeft();
1180 else if ( winRect
.GetRight() > viewRect
.GetWidth() )
1182 diff
= winRect
.GetRight() - viewRect
.GetWidth() + 1;
1183 // round up to next scroll step if we can't get exact position,
1184 // so that the window is fully visible:
1188 startx
= (startx
* stepx
+ diff
) / stepx
;
1191 Scroll(startx
, starty
);
1195 #ifdef wxHAS_GENERIC_SCROLLWIN
1197 // ----------------------------------------------------------------------------
1198 // wxScrollHelper implementation
1199 // ----------------------------------------------------------------------------
1201 wxScrollHelper::wxScrollHelper(wxWindow
*winToScroll
)
1202 : wxScrollHelperBase(winToScroll
)
1205 m_yVisibility
= wxSHOW_SB_DEFAULT
;
1208 bool wxScrollHelper::IsScrollbarShown(int orient
) const
1210 wxScrollbarVisibility visibility
= orient
== wxHORIZONTAL
? m_xVisibility
1213 return visibility
!= wxSHOW_SB_NEVER
;
1216 void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz
,
1217 wxScrollbarVisibility vert
)
1219 if ( horz
!= m_xVisibility
|| vert
!= m_yVisibility
)
1221 m_xVisibility
= horz
;
1222 m_yVisibility
= vert
;
1229 wxScrollHelper::DoAdjustScrollbar(int orient
,
1234 int& scrollPosition
,
1235 int& scrollLinesPerPage
,
1236 wxScrollbarVisibility visibility
)
1238 // scroll lines per page: if 0, no scrolling is needed
1239 // check if we need scrollbar in this direction at all
1240 if ( pixelsPerUnit
== 0 || clientSize
>= virtSize
)
1242 // scrolling is disabled or unnecessary
1245 scrollLinesPerPage
= 0;
1247 else // might need scrolling
1249 // Round up integer division to catch any "leftover" client space.
1250 scrollUnits
= (virtSize
+ pixelsPerUnit
- 1) / pixelsPerUnit
;
1252 // Calculate the number of fully scroll units
1253 scrollLinesPerPage
= clientSize
/ pixelsPerUnit
;
1255 if ( scrollLinesPerPage
>= scrollUnits
)
1257 // we're big enough to not need scrolling
1260 scrollLinesPerPage
= 0;
1262 else // we do need a scrollbar
1264 if ( scrollLinesPerPage
< 1 )
1265 scrollLinesPerPage
= 1;
1267 // Correct position if greater than extent of canvas minus
1268 // the visible portion of it or if below zero
1269 const int posMax
= scrollUnits
- scrollLinesPerPage
;
1270 if ( scrollPosition
> posMax
)
1271 scrollPosition
= posMax
;
1272 else if ( scrollPosition
< 0 )
1277 // in wxSHOW_SB_NEVER case don't show the scrollbar even if it's needed, in
1278 // wxSHOW_SB_ALWAYS case show the scrollbar even if it's not needed by
1279 // passing a special range value to SetScrollbar()
1281 switch ( visibility
)
1283 case wxSHOW_SB_NEVER
:
1287 case wxSHOW_SB_ALWAYS
:
1288 range
= scrollUnits
? scrollUnits
: -1;
1292 wxFAIL_MSG( wxS("unknown scrollbar visibility") );
1295 case wxSHOW_SB_DEFAULT
:
1296 range
= scrollUnits
;
1301 m_win
->SetScrollbar(orient
, scrollPosition
, scrollLinesPerPage
, range
);
1304 void wxScrollHelper::AdjustScrollbars()
1306 static wxRecursionGuardFlag s_flagReentrancy
;
1307 wxRecursionGuard
guard(s_flagReentrancy
);
1308 if ( guard
.IsInside() )
1310 // don't reenter AdjustScrollbars() while another call to
1311 // AdjustScrollbars() is in progress because this may lead to calling
1312 // ScrollWindow() twice and this can really happen under MSW if
1313 // SetScrollbar() call below adds or removes the scrollbar which
1314 // changes the window size and hence results in another
1315 // AdjustScrollbars() call
1319 int oldXScroll
= m_xScrollPosition
;
1320 int oldYScroll
= m_yScrollPosition
;
1322 // we may need to readjust the scrollbars several times as enabling one of
1323 // them reduces the area available for the window contents and so can make
1324 // the other scrollbar necessary now although it wasn't necessary before
1326 // VZ: normally this loop should be over in at most 2 iterations, I don't
1327 // know why do we need 5 of them
1328 for ( int iterationCount
= 0; iterationCount
< 5; iterationCount
++ )
1330 wxSize clientSize
= GetTargetSize();
1331 const wxSize virtSize
= m_targetWindow
->GetVirtualSize();
1333 // this block of code tries to work around the following problem: the
1334 // window could have been just resized to have enough space to show its
1335 // full contents without the scrollbars, but its client size could be
1336 // not big enough because it does have the scrollbars right now and so
1337 // the scrollbars would remain even though we don't need them any more
1339 // to prevent this from happening, check if we have enough space for
1340 // everything without the scrollbars and explicitly disable them then
1341 const wxSize availSize
= GetSizeAvailableForScrollTarget(
1342 m_win
->GetSize() - m_win
->GetWindowBorderSize());
1343 if ( availSize
!= clientSize
)
1345 if ( availSize
.x
>= virtSize
.x
&& availSize
.y
>= virtSize
.y
)
1347 // this will be enough to make the scrollbars disappear below
1348 // and then the client size will indeed become equal to the
1349 // full available size
1350 clientSize
= availSize
;
1355 DoAdjustScrollbar(wxHORIZONTAL
,
1358 m_xScrollPixelsPerLine
,
1361 m_xScrollLinesPerPage
,
1364 DoAdjustScrollbar(wxVERTICAL
,
1367 m_yScrollPixelsPerLine
,
1370 m_yScrollLinesPerPage
,
1374 // If a scrollbar (dis)appeared as a result of this, we need to adjust
1375 // them again but if the client size didn't change, then we're done
1376 if ( GetTargetSize() == clientSize
)
1381 // Sorry, some Motif-specific code to implement a backing pixmap
1382 // for the wxRETAINED style. Implementing a backing store can't
1383 // be entirely generic because it relies on the wxWindowDC implementation
1384 // to duplicate X drawing calls for the backing pixmap.
1386 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
1388 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
1390 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
1391 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
1392 if (m_targetWindow
->GetBackingPixmap() &&
1393 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
1394 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
1396 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
1397 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
1400 if (!m_targetWindow
->GetBackingPixmap() &&
1401 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
1403 int depth
= wxDisplayDepth();
1404 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
1405 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
1406 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
1407 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
1413 if (oldXScroll
!= m_xScrollPosition
)
1415 if (m_xScrollingEnabled
)
1416 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
1419 m_targetWindow
->Refresh(true, GetScrollRect());
1422 if (oldYScroll
!= m_yScrollPosition
)
1424 if (m_yScrollingEnabled
)
1425 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
1428 m_targetWindow
->Refresh(true, GetScrollRect());
1432 void wxScrollHelper::DoScroll( int x_pos
, int y_pos
)
1434 if (!m_targetWindow
)
1437 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
1438 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
1441 GetTargetSize(&w
, &h
);
1443 // compute new position:
1444 int new_x
= m_xScrollPosition
;
1445 int new_y
= m_yScrollPosition
;
1447 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
1451 // Calculate page size i.e. number of scroll units you get on the
1452 // current client window
1453 int noPagePositions
= w
/m_xScrollPixelsPerLine
;
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_x
= wxMin( m_xScrollLines
-noPagePositions
, new_x
);
1459 new_x
= wxMax( 0, new_x
);
1461 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
1465 // Calculate page size i.e. number of scroll units you get on the
1466 // current client window
1467 int noPagePositions
= h
/m_yScrollPixelsPerLine
;
1468 if (noPagePositions
< 1) noPagePositions
= 1;
1470 // Correct position if greater than extent of canvas minus
1471 // the visible portion of it or if below zero
1472 new_y
= wxMin( m_yScrollLines
-noPagePositions
, new_y
);
1473 new_y
= wxMax( 0, new_y
);
1476 if ( new_x
== m_xScrollPosition
&& new_y
== m_yScrollPosition
)
1477 return; // nothing to do, the position didn't change
1479 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
1480 // otherwise invalidated area could be updated incorrectly later when
1481 // ScrollWindow() makes sure they're repainted before scrolling them
1482 m_targetWindow
->Update();
1484 // update the position and scroll the window now:
1485 if (m_xScrollPosition
!= new_x
)
1487 int old_x
= m_xScrollPosition
;
1488 m_xScrollPosition
= new_x
;
1489 m_win
->SetScrollPos( wxHORIZONTAL
, new_x
);
1490 m_targetWindow
->ScrollWindow( (old_x
-new_x
)*m_xScrollPixelsPerLine
, 0,
1494 if (m_yScrollPosition
!= new_y
)
1496 int old_y
= m_yScrollPosition
;
1497 m_yScrollPosition
= new_y
;
1498 m_win
->SetScrollPos( wxVERTICAL
, new_y
);
1499 m_targetWindow
->ScrollWindow( 0, (old_y
-new_y
)*m_yScrollPixelsPerLine
,
1504 #endif // wxHAS_GENERIC_SCROLLWIN
1506 // ----------------------------------------------------------------------------
1507 // wxScrolled<T> and wxScrolledWindow implementation
1508 // ----------------------------------------------------------------------------
1510 wxSize
wxScrolledT_Helper::FilterBestSize(const wxWindow
*win
,
1511 const wxScrollHelper
*helper
,
1512 const wxSize
& origBest
)
1514 // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1515 // all scrollable windows should behave like this, only those that
1516 // contain children controls within scrollable area
1517 // (i.e., wxScrolledWindow) and other some scrollable windows may
1518 // have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1520 wxSize best
= origBest
;
1522 if ( win
->GetAutoLayout() )
1524 // Only use the content to set the window size in the direction
1525 // where there's no scrolling; otherwise we're going to get a huge
1526 // window in the direction in which scrolling is enabled
1528 helper
->GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1530 // NB: This code used to use *current* size if min size wasn't
1531 // specified, presumably to get some reasonable (i.e., larger than
1532 // minimal) size. But that's a wrong thing to do in GetBestSize(),
1533 // so we use minimal size as specified. If the app needs some
1534 // minimal size for its scrolled window, it should set it and put
1535 // the window into sizer as expandable so that it can use all space
1538 // See also http://svn.wxwidgets.org/viewvc/wx?view=rev&revision=45864
1540 wxSize minSize
= win
->GetMinSize();
1543 best
.x
= minSize
.x
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1546 best
.y
= minSize
.y
+ wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
1553 WXLRESULT
wxScrolledT_Helper::FilterMSWWindowProc(WXUINT nMsg
, WXLRESULT rc
)
1556 // we need to process arrows ourselves for scrolling
1557 if ( nMsg
== WM_GETDLGCODE
)
1559 rc
|= DLGC_WANTARROWS
;
1566 // NB: skipping wxScrolled<T> in wxRTTI information because being a templte,
1567 // it doesn't and can't implement wxRTTI support
1568 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)