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
);
82 void ResetDrawnFlag() { m_hasDrawnWindow
= false; }
85 wxScrollHelperBase
*m_scrollHelper
;
87 bool m_hasDrawnWindow
;
89 wxDECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler
);
93 // ----------------------------------------------------------------------------
94 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
95 // a captured mouse is held outside the window
96 // ----------------------------------------------------------------------------
98 class wxAutoScrollTimer
: public wxTimer
101 wxAutoScrollTimer(wxWindow
*winToScroll
,
102 wxScrollHelperBase
*scroll
,
103 wxEventType eventTypeToSend
,
104 int pos
, int orient
);
106 virtual void Notify();
110 wxScrollHelperBase
*m_scrollHelper
;
111 wxEventType m_eventType
;
115 wxDECLARE_NO_COPY_CLASS(wxAutoScrollTimer
);
118 // ============================================================================
120 // ============================================================================
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow
*winToScroll
,
127 wxScrollHelperBase
*scroll
,
128 wxEventType eventTypeToSend
,
132 m_scrollHelper
= scroll
;
133 m_eventType
= eventTypeToSend
;
138 void wxAutoScrollTimer::Notify()
140 // only do all this as long as the window is capturing the mouse
141 if ( wxWindow::GetCapture() != m_win
)
145 else // we still capture the mouse, continue generating events
147 // first scroll the window if we are allowed to do it
148 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
149 event1
.SetEventObject(m_win
);
150 if ( m_scrollHelper
->SendAutoScrollEvents(event1
) &&
151 m_win
->GetEventHandler()->ProcessEvent(event1
) )
153 // and then send a pseudo mouse-move event to refresh the selection
154 wxMouseEvent
event2(wxEVT_MOTION
);
155 event2
.SetPosition(wxGetMousePosition());
157 // the mouse event coordinates should be client, not screen as
158 // returned by wxGetMousePosition
159 wxWindow
*parentTop
= m_win
;
160 while ( parentTop
->GetParent() )
161 parentTop
= parentTop
->GetParent();
162 wxPoint ptOrig
= parentTop
->GetPosition();
163 event2
.m_x
-= ptOrig
.x
;
164 event2
.m_y
-= ptOrig
.y
;
166 event2
.SetEventObject(m_win
);
168 // FIXME: we don't fill in the other members - ok?
170 m_win
->GetEventHandler()->ProcessEvent(event2
);
172 else // can't scroll further, stop
180 // ----------------------------------------------------------------------------
181 // wxScrollHelperEvtHandler
182 // ----------------------------------------------------------------------------
184 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
186 wxEventType evType
= event
.GetEventType();
188 // the explanation of wxEVT_PAINT processing hack: for historic reasons
189 // there are 2 ways to process this event in classes deriving from
190 // wxScrolledWindow. The user code may
192 // 1. override wxScrolledWindow::OnDraw(dc)
193 // 2. define its own OnPaint() handler
195 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
196 // always processes the draw event, so we can't just try the window
197 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
198 // (the latter would never be called in wxUniversal).
200 // So the solution is to have a flag telling us whether the user code drew
201 // anything in the window. We set it to true here but reset it to false in
202 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
203 // user code defined OnPaint() in the derived class)
204 m_hasDrawnWindow
= true;
206 // pass it on to the real handler
207 bool processed
= wxEvtHandler::ProcessEvent(event
);
209 // always process the size events ourselves, even if the user code handles
210 // them as well, as we need to AdjustScrollbars()
212 // NB: it is important to do it after processing the event in the normal
213 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
214 // scrollbar[s] (dis)appear and it should be seen by the user code
216 if ( evType
== wxEVT_SIZE
)
218 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
225 // normally, nothing more to do here - except if it was a paint event
226 // which wasn't really processed, then we'll try to call our
227 // OnDraw() below (from HandleOnPaint)
228 if ( m_hasDrawnWindow
|| event
.IsCommandEvent() )
234 if ( evType
== wxEVT_PAINT
)
236 m_scrollHelper
->HandleOnPaint((wxPaintEvent
&)event
);
240 if ( evType
== wxEVT_CHILD_FOCUS
)
242 m_scrollHelper
->HandleOnChildFocus((wxChildFocusEvent
&)event
);
246 // reset the skipped flag (which might have been set to true in
247 // ProcessEvent() above) to be able to test it below
248 bool wasSkipped
= event
.GetSkipped();
252 if ( evType
== wxEVT_SCROLLWIN_TOP
||
253 evType
== wxEVT_SCROLLWIN_BOTTOM
||
254 evType
== wxEVT_SCROLLWIN_LINEUP
||
255 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
256 evType
== wxEVT_SCROLLWIN_PAGEUP
||
257 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
258 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
259 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
261 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
262 if ( !event
.GetSkipped() )
264 // it makes sense to indicate that we processed the message as we
265 // did scroll the window (and also notice that wxAutoScrollTimer
266 // relies on our return value to stop scrolling when we are at top
267 // or bottom already)
273 if ( evType
== wxEVT_ENTER_WINDOW
)
275 m_scrollHelper
->HandleOnMouseEnter((wxMouseEvent
&)event
);
277 else if ( evType
== wxEVT_LEAVE_WINDOW
)
279 m_scrollHelper
->HandleOnMouseLeave((wxMouseEvent
&)event
);
282 // Use GTK's own scroll wheel handling in GtkScrolledWindow
284 else if ( evType
== wxEVT_MOUSEWHEEL
)
286 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
290 #endif // wxUSE_MOUSEWHEEL
291 else if ( evType
== wxEVT_CHAR
)
293 m_scrollHelper
->HandleOnChar((wxKeyEvent
&)event
);
294 if ( !event
.GetSkipped() )
302 event
.Skip(wasSkipped
);
307 // ============================================================================
308 // wxScrollHelperBase implementation
309 // ============================================================================
311 // ----------------------------------------------------------------------------
312 // wxScrollHelperBase construction
313 // ----------------------------------------------------------------------------
315 wxScrollHelperBase::wxScrollHelperBase(wxWindow
*win
)
317 wxASSERT_MSG( win
, _T("associated window can't be NULL in wxScrollHelper") );
319 m_xScrollPixelsPerLine
=
320 m_yScrollPixelsPerLine
=
325 m_xScrollLinesPerPage
=
326 m_yScrollLinesPerPage
= 0;
328 m_xScrollingEnabled
=
329 m_yScrollingEnabled
= true;
338 m_targetWindow
= NULL
;
340 m_timerAutoScroll
= NULL
;
346 m_win
->SetScrollHelper(static_cast<wxScrollHelper
*>(this));
348 // by default, the associated window is also the target window
349 DoSetTargetWindow(win
);
352 wxScrollHelperBase::~wxScrollHelperBase()
359 // ----------------------------------------------------------------------------
360 // setting scrolling parameters
361 // ----------------------------------------------------------------------------
363 void wxScrollHelperBase::SetScrollbars(int pixelsPerUnitX
,
373 CalcUnscrolledPosition(xPos
, yPos
, &xpos
, &ypos
);
376 (noUnitsX
!= 0 && m_xScrollLines
== 0) ||
377 (noUnitsX
< m_xScrollLines
&& xpos
> pixelsPerUnitX
* noUnitsX
) ||
379 (noUnitsY
!= 0 && m_yScrollLines
== 0) ||
380 (noUnitsY
< m_yScrollLines
&& ypos
> pixelsPerUnitY
* noUnitsY
) ||
381 (xPos
!= m_xScrollPosition
) ||
382 (yPos
!= m_yScrollPosition
)
385 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
386 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
387 m_xScrollPosition
= xPos
;
388 m_yScrollPosition
= yPos
;
390 int w
= noUnitsX
* pixelsPerUnitX
;
391 int h
= noUnitsY
* pixelsPerUnitY
;
393 // For better backward compatibility we set persisting limits
394 // here not just the size. It makes SetScrollbars 'sticky'
395 // emulating the old non-autoscroll behaviour.
396 // m_targetWindow->SetVirtualSizeHints( w, h );
398 // The above should arguably be deprecated, this however we still need.
400 // take care not to set 0 virtual size, 0 means that we don't have any
401 // scrollbars and hence we should use the real size instead of the virtual
402 // one which is indicated by using wxDefaultCoord
403 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
404 h
? h
: wxDefaultCoord
);
406 if (do_refresh
&& !noRefresh
)
407 m_targetWindow
->Refresh(true, GetScrollRect());
409 #ifndef __WXUNIVERSAL__
410 // If the target is not the same as the window with the scrollbars,
411 // then we need to update the scrollbars here, since they won't have
412 // been updated by SetVirtualSize().
413 if ( m_targetWindow
!= m_win
)
414 #endif // !__WXUNIVERSAL__
418 #ifndef __WXUNIVERSAL__
421 // otherwise this has been done by AdjustScrollbars, above
423 #endif // !__WXUNIVERSAL__
426 // ----------------------------------------------------------------------------
427 // [target] window handling
428 // ----------------------------------------------------------------------------
430 void wxScrollHelperBase::DeleteEvtHandler()
432 // search for m_handler in the handler list
433 if ( m_win
&& m_handler
)
435 if ( m_win
->RemoveEventHandler(m_handler
) )
439 //else: something is very wrong, so better [maybe] leak memory than
440 // risk a crash because of double deletion
446 void wxScrollHelperBase::ResetDrawnFlag()
448 wxCHECK_RET( m_handler
, "invalid use of ResetDrawnFlag - no handler?" );
449 m_handler
->ResetDrawnFlag();
452 void wxScrollHelperBase::DoSetTargetWindow(wxWindow
*target
)
454 m_targetWindow
= target
;
456 target
->MacSetClipChildren( true ) ;
459 // install the event handler which will intercept the events we're
460 // interested in (but only do it for our real window, not the target window
461 // which we scroll - we don't need to hijack its events)
462 if ( m_targetWindow
== m_win
)
464 // if we already have a handler, delete it first
467 m_handler
= new wxScrollHelperEvtHandler(this);
468 m_targetWindow
->PushEventHandler(m_handler
);
472 void wxScrollHelperBase::SetTargetWindow(wxWindow
*target
)
474 wxCHECK_RET( target
, wxT("target window must not be NULL") );
476 if ( target
== m_targetWindow
)
479 DoSetTargetWindow(target
);
482 wxWindow
*wxScrollHelperBase::GetTargetWindow() const
484 return m_targetWindow
;
487 // ----------------------------------------------------------------------------
488 // scrolling implementation itself
489 // ----------------------------------------------------------------------------
491 void wxScrollHelperBase::HandleOnScroll(wxScrollWinEvent
& event
)
493 int nScrollInc
= CalcScrollInc(event
);
494 if ( nScrollInc
== 0 )
496 // can't scroll further
502 bool needsRefresh
= false;
505 int orient
= event
.GetOrientation();
506 if (orient
== wxHORIZONTAL
)
508 if ( m_xScrollingEnabled
)
510 dx
= -m_xScrollPixelsPerLine
* nScrollInc
;
519 if ( m_yScrollingEnabled
)
521 dy
= -m_yScrollPixelsPerLine
* nScrollInc
;
531 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
532 // otherwise invalidated area could be updated incorrectly later when
533 // ScrollWindow() makes sure they're repainted before scrolling them
535 // wxWindowMac is taking care of making sure the update area is correctly
536 // set up, while not forcing an immediate redraw
538 m_targetWindow
->Update();
542 if (orient
== wxHORIZONTAL
)
544 m_xScrollPosition
+= nScrollInc
;
545 m_win
->SetScrollPos(wxHORIZONTAL
, m_xScrollPosition
);
549 m_yScrollPosition
+= nScrollInc
;
550 m_win
->SetScrollPos(wxVERTICAL
, m_yScrollPosition
);
555 m_targetWindow
->Refresh(true, GetScrollRect());
559 m_targetWindow
->ScrollWindow(dx
, dy
, GetScrollRect());
563 int wxScrollHelperBase::CalcScrollInc(wxScrollWinEvent
& event
)
565 int pos
= event
.GetPosition();
566 int orient
= event
.GetOrientation();
569 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
571 if (orient
== wxHORIZONTAL
)
572 nScrollInc
= - m_xScrollPosition
;
574 nScrollInc
= - m_yScrollPosition
;
576 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
578 if (orient
== wxHORIZONTAL
)
579 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
581 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
583 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
587 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
591 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
593 if (orient
== wxHORIZONTAL
)
594 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
596 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
598 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
600 if (orient
== wxHORIZONTAL
)
601 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
603 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
605 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
606 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
608 if (orient
== wxHORIZONTAL
)
609 nScrollInc
= pos
- m_xScrollPosition
;
611 nScrollInc
= pos
- m_yScrollPosition
;
614 if (orient
== wxHORIZONTAL
)
616 if ( m_xScrollPosition
+ nScrollInc
< 0 )
618 // As -ve as we can go
619 nScrollInc
= -m_xScrollPosition
;
621 else // check for the other bound
623 const int posMax
= m_xScrollLines
- m_xScrollLinesPerPage
;
624 if ( m_xScrollPosition
+ nScrollInc
> posMax
)
626 // As +ve as we can go
627 nScrollInc
= posMax
- m_xScrollPosition
;
633 if ( m_yScrollPosition
+ nScrollInc
< 0 )
635 // As -ve as we can go
636 nScrollInc
= -m_yScrollPosition
;
638 else // check for the other bound
640 const int posMax
= m_yScrollLines
- m_yScrollLinesPerPage
;
641 if ( m_yScrollPosition
+ nScrollInc
> posMax
)
643 // As +ve as we can go
644 nScrollInc
= posMax
- m_yScrollPosition
;
652 void wxScrollHelperBase::DoPrepareDC(wxDC
& dc
)
654 wxPoint pt
= dc
.GetDeviceOrigin();
656 // It may actually be correct to always query
657 // the m_sign from the DC here, but I leave the
658 // #ifdef GTK for now.
659 if (m_win
->GetLayoutDirection() == wxLayout_RightToLeft
)
660 dc
.SetDeviceOrigin( pt
.x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
,
661 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
664 dc
.SetDeviceOrigin( pt
.x
- m_xScrollPosition
* m_xScrollPixelsPerLine
,
665 pt
.y
- m_yScrollPosition
* m_yScrollPixelsPerLine
);
666 dc
.SetUserScale( m_scaleX
, m_scaleY
);
669 void wxScrollHelperBase::SetScrollRate( int xstep
, int ystep
)
671 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
672 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
674 m_xScrollPixelsPerLine
= xstep
;
675 m_yScrollPixelsPerLine
= ystep
;
677 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
678 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
680 m_win
->SetScrollPos( wxHORIZONTAL
, m_xScrollPosition
);
681 m_win
->SetScrollPos( wxVERTICAL
, m_yScrollPosition
);
682 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
687 void wxScrollHelperBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
690 *x_unit
= m_xScrollPixelsPerLine
;
692 *y_unit
= m_yScrollPixelsPerLine
;
696 int wxScrollHelperBase::GetScrollLines( int orient
) const
698 if ( orient
== wxHORIZONTAL
)
699 return m_xScrollLines
;
701 return m_yScrollLines
;
704 int wxScrollHelperBase::GetScrollPageSize(int orient
) const
706 if ( orient
== wxHORIZONTAL
)
707 return m_xScrollLinesPerPage
;
709 return m_yScrollLinesPerPage
;
712 void wxScrollHelperBase::SetScrollPageSize(int orient
, int pageSize
)
714 if ( orient
== wxHORIZONTAL
)
715 m_xScrollLinesPerPage
= pageSize
;
717 m_yScrollLinesPerPage
= pageSize
;
720 void wxScrollHelperBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
722 m_xScrollingEnabled
= x_scroll
;
723 m_yScrollingEnabled
= y_scroll
;
726 // Where the current view starts from
727 void wxScrollHelperBase::DoGetViewStart (int *x
, int *y
) const
730 *x
= m_xScrollPosition
;
732 *y
= m_yScrollPosition
;
735 void wxScrollHelperBase::DoCalcScrolledPosition(int x
, int y
,
736 int *xx
, int *yy
) const
739 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
741 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
744 void wxScrollHelperBase::DoCalcUnscrolledPosition(int x
, int y
,
745 int *xx
, int *yy
) const
748 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
750 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
753 // ----------------------------------------------------------------------------
755 // ----------------------------------------------------------------------------
757 bool wxScrollHelperBase::ScrollLayout()
759 if ( m_win
->GetSizer() && m_targetWindow
== m_win
)
761 // If we're the scroll target, take into account the
762 // virtual size and scrolled position of the window.
764 int x
= 0, y
= 0, w
= 0, h
= 0;
765 CalcScrolledPosition(0,0, &x
,&y
);
766 m_win
->GetVirtualSize(&w
, &h
);
767 m_win
->GetSizer()->SetDimension(x
, y
, w
, h
);
771 // fall back to default for LayoutConstraints
772 return m_win
->wxWindow::Layout();
775 void wxScrollHelperBase::ScrollDoSetVirtualSize(int x
, int y
)
777 m_win
->wxWindow::DoSetVirtualSize( x
, y
);
780 if (m_win
->GetAutoLayout())
784 // wxWindow's GetBestVirtualSize returns the actual window size,
785 // whereas we want to return the virtual size
786 wxSize
wxScrollHelperBase::ScrollGetBestVirtualSize() const
788 wxSize
clientSize(m_win
->GetClientSize());
789 if ( m_win
->GetSizer() )
790 clientSize
.IncTo(m_win
->GetSizer()->CalcMin());
795 // ----------------------------------------------------------------------------
797 // ----------------------------------------------------------------------------
799 // Default OnSize resets scrollbars, if any
800 void wxScrollHelperBase::HandleOnSize(wxSizeEvent
& WXUNUSED(event
))
802 if ( m_targetWindow
->GetAutoLayout() )
804 wxSize size
= m_targetWindow
->GetBestVirtualSize();
806 // This will call ::Layout() and ::AdjustScrollbars()
807 m_win
->SetVirtualSize( size
);
815 // This calls OnDraw, having adjusted the origin according to the current
817 void wxScrollHelperBase::HandleOnPaint(wxPaintEvent
& WXUNUSED(event
))
819 // don't use m_targetWindow here, this is always called for ourselves
826 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
827 // compatibility here - if we used OnKeyDown(), the programs which process
828 // arrows themselves in their OnChar() would never get the message and like
829 // this they always have the priority
830 void wxScrollHelperBase::HandleOnChar(wxKeyEvent
& event
)
832 int stx
= 0, sty
= 0, // view origin
833 szx
= 0, szy
= 0, // view size (total)
834 clix
= 0, cliy
= 0; // view size (on screen)
836 GetViewStart(&stx
, &sty
);
837 GetTargetSize(&clix
, &cliy
);
838 m_targetWindow
->GetVirtualSize(&szx
, &szy
);
840 if( m_xScrollPixelsPerLine
)
842 clix
/= m_xScrollPixelsPerLine
;
843 szx
/= m_xScrollPixelsPerLine
;
850 if( m_yScrollPixelsPerLine
)
852 cliy
/= m_yScrollPixelsPerLine
;
853 szy
/= m_yScrollPixelsPerLine
;
861 int xScrollOld
= m_xScrollPosition
,
862 yScrollOld
= m_yScrollPosition
;
865 switch ( event
.GetKeyCode() )
868 dsty
= sty
- (5 * cliy
/ 6);
869 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
873 Scroll(-1, sty
+ (5 * cliy
/ 6));
877 Scroll(0, event
.ControlDown() ? 0 : -1);
881 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
905 if ( m_xScrollPosition
!= xScrollOld
)
907 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBTRACK
, m_xScrollPosition
,
909 evt
.SetEventObject(m_win
);
910 m_win
->GetEventHandler()->ProcessEvent(evt
);
913 if ( m_yScrollPosition
!= yScrollOld
)
915 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBTRACK
, m_yScrollPosition
,
917 evt
.SetEventObject(m_win
);
918 m_win
->GetEventHandler()->ProcessEvent(evt
);
922 // ----------------------------------------------------------------------------
923 // autoscroll stuff: these functions deal with sending fake scroll events when
924 // a captured mouse is being held outside the window
925 // ----------------------------------------------------------------------------
927 bool wxScrollHelperBase::SendAutoScrollEvents(wxScrollWinEvent
& event
) const
929 // only send the event if the window is scrollable in this direction
930 wxWindow
*win
= (wxWindow
*)event
.GetEventObject();
931 return win
->HasScrollbar(event
.GetOrientation());
934 void wxScrollHelperBase::StopAutoScrolling()
937 if ( m_timerAutoScroll
)
939 delete m_timerAutoScroll
;
940 m_timerAutoScroll
= NULL
;
945 void wxScrollHelperBase::HandleOnMouseEnter(wxMouseEvent
& event
)
952 void wxScrollHelperBase::HandleOnMouseLeave(wxMouseEvent
& event
)
954 // don't prevent the usual processing of the event from taking place
957 // when a captured mouse leave a scrolled window we start generate
958 // scrolling events to allow, for example, extending selection beyond the
959 // visible area in some controls
960 if ( wxWindow::GetCapture() == m_targetWindow
)
962 // where is the mouse leaving?
964 wxPoint pt
= event
.GetPosition();
967 orient
= wxHORIZONTAL
;
975 else // we're lower or to the right of the window
977 wxSize size
= m_targetWindow
->GetClientSize();
980 orient
= wxHORIZONTAL
;
981 pos
= m_xScrollLines
;
983 else if ( pt
.y
> size
.y
)
986 pos
= m_yScrollLines
;
988 else // this should be impossible
990 // but seems to happen sometimes under wxMSW - maybe it's a bug
991 // there but for now just ignore it
993 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
999 // only start the auto scroll timer if the window can be scrolled in
1001 if ( !m_targetWindow
->HasScrollbar(orient
) )
1005 delete m_timerAutoScroll
;
1006 m_timerAutoScroll
= new wxAutoScrollTimer
1008 m_targetWindow
, this,
1009 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1010 : wxEVT_SCROLLWIN_LINEDOWN
,
1014 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1021 #if wxUSE_MOUSEWHEEL
1023 void wxScrollHelperBase::HandleOnMouseWheel(wxMouseEvent
& event
)
1025 m_wheelRotation
+= event
.GetWheelRotation();
1026 int lines
= m_wheelRotation
/ event
.GetWheelDelta();
1027 m_wheelRotation
-= lines
* event
.GetWheelDelta();
1032 wxScrollWinEvent newEvent
;
1034 newEvent
.SetPosition(0);
1035 newEvent
.SetOrientation( event
.GetWheelAxis() == 0 ? wxVERTICAL
: wxHORIZONTAL
);
1036 newEvent
.SetEventObject(m_win
);
1038 if (event
.IsPageScroll())
1041 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEUP
);
1043 newEvent
.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN
);
1045 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1049 lines
*= event
.GetLinesPerAction();
1051 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEUP
);
1053 newEvent
.SetEventType(wxEVT_SCROLLWIN_LINEDOWN
);
1055 int times
= abs(lines
);
1056 for (; times
> 0; times
--)
1057 m_win
->GetEventHandler()->ProcessEvent(newEvent
);
1062 #endif // wxUSE_MOUSEWHEEL
1064 void wxScrollHelperBase::HandleOnChildFocus(wxChildFocusEvent
& event
)
1066 // this event should be processed by all windows in parenthood chain,
1067 // e.g. so that nested wxScrolledWindows work correctly
1070 // find the immediate child under which the window receiving focus is:
1071 wxWindow
*win
= event
.GetWindow();
1073 if ( win
== m_targetWindow
)
1074 return; // nothing to do
1077 if (wxDynamicCast(win
, wxScrollBar
))
1081 // Fixing ticket: http://trac.wxwidgets.org/ticket/9563
1082 // When a child inside a wxControlContainer receives a focus, the
1083 // wxControlContainer generates an artificial wxChildFocusEvent for
1084 // itself, telling its parent that 'it' received the focus. The effect is
1085 // that this->HandleOnChildFocus is called twice, first with the
1086 // artificial wxChildFocusEvent and then with the original event. We need
1087 // to ignore the artificial event here or otherwise HandleOnChildFocus
1088 // would first scroll the target window to make the entire
1089 // wxControlContainer visible and immediately afterwards scroll the target
1090 // window again to make the child widget visible. This leads to ugly
1091 // flickering when using nested wxPanels/wxScrolledWindows.
1093 // Ignore this event if 'win' is derived from wxControlContainer AND its
1094 // parent is the m_targetWindow AND 'win' is not actually reciving the
1095 // focus (win != FindFocus). TODO: This affects all wxControlContainer
1096 // objects, but wxControlContainer is not part of the wxWidgets RTTI and
1097 // so wxDynamicCast(win, wxControlContainer) does not compile. Find a way
1098 // to determine if 'win' derives from wxControlContainer. Until then,
1099 // testing if 'win' derives from wxPanel will probably get >90% of all
1102 wxWindow
*actual_focus
=wxWindow::FindFocus();
1103 if (win
!= actual_focus
&&
1104 wxDynamicCast(win
, wxPanel
) != 0 &&
1105 win
->GetParent() == m_targetWindow
)
1106 // if win is a wxPanel and receives the focus, it should not be
1107 // scrolled into view
1110 const wxRect
viewRect(m_targetWindow
->GetClientRect());
1112 // For composite controls such as wxComboCtrl we should try to fit the
1113 // entire control inside the visible area of the target window, not just
1114 // the focused child of the control. Otherwise we'd make only the textctrl
1115 // part of a wxComboCtrl visible and the button would still be outside the
1116 // scrolled area. But do so only if the parent fits *entirely* inside the
1117 // scrolled window. In other situations, such as nested wxPanel or
1118 // wxScrolledWindows, the parent might be way to big to fit inside the
1119 // scrolled window. If that is the case, then make only the focused window
1121 if ( win
->GetParent() != m_targetWindow
)
1123 wxWindow
*parent
=win
->GetParent();
1124 wxSize parent_size
=parent
->GetSize();
1125 if (parent_size
.GetWidth() <= viewRect
.GetWidth() &&
1126 parent_size
.GetHeight() <= viewRect
.GetHeight())
1127 // make the immediate parent visible instead of the focused control
1131 // make win position relative to the m_targetWindow viewing area instead of
1134 winRect(m_targetWindow
->ScreenToClient(win
->GetScreenPosition()),
1137 // check if it's fully visible
1138 if ( viewRect
.Contains(winRect
) )
1140 // it is, nothing to do
1144 // check if we can make it fully visible: this is only possible if it's not
1145 // larger than our view area
1146 if ( winRect
.GetWidth() > viewRect
.GetWidth() ||
1147 winRect
.GetHeight() > viewRect
.GetHeight() )
1149 // we can't make it fit so avoid scrolling it at all, this is only
1150 // going to be confusing and not helpful
1155 // do make the window fit inside the view area by scrolling to it
1157 GetScrollPixelsPerUnit(&stepx
, &stepy
);
1160 GetViewStart(&startx
, &starty
);
1162 // first in vertical direction:
1167 if ( winRect
.GetTop() < 0 )
1169 diff
= winRect
.GetTop();
1171 else if ( winRect
.GetBottom() > viewRect
.GetHeight() )
1173 diff
= winRect
.GetBottom() - viewRect
.GetHeight() + 1;
1174 // round up to next scroll step if we can't get exact position,
1175 // so that the window is fully visible:
1179 starty
= (starty
* stepy
+ diff
) / stepy
;
1187 if ( winRect
.GetLeft() < 0 )
1189 diff
= winRect
.GetLeft();
1191 else if ( winRect
.GetRight() > viewRect
.GetWidth() )
1193 diff
= winRect
.GetRight() - viewRect
.GetWidth() + 1;
1194 // round up to next scroll step if we can't get exact position,
1195 // so that the window is fully visible:
1199 startx
= (startx
* stepx
+ diff
) / stepx
;
1202 Scroll(startx
, starty
);
1206 #ifdef wxHAS_GENERIC_SCROLLWIN
1208 // ----------------------------------------------------------------------------
1209 // wxScrollHelper implementation
1210 // ----------------------------------------------------------------------------
1212 wxScrollHelper::wxScrollHelper(wxWindow
*winToScroll
)
1213 : wxScrollHelperBase(winToScroll
)
1216 m_yVisibility
= wxSHOW_SB_DEFAULT
;
1219 void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz
,
1220 wxScrollbarVisibility vert
)
1222 if ( horz
!= m_xVisibility
|| vert
!= m_yVisibility
)
1224 m_xVisibility
= horz
;
1225 m_yVisibility
= vert
;
1232 wxScrollHelper::DoAdjustScrollbar(int orient
,
1237 int& scrollPosition
,
1238 int& scrollLinesPerPage
,
1239 wxScrollbarVisibility visibility
)
1241 // scroll lines per page: if 0, no scrolling is needed
1242 // check if we need scrollbar in this direction at all
1243 if ( pixelsPerUnit
== 0 || clientSize
>= virtSize
)
1245 // scrolling is disabled or unnecessary
1248 scrollLinesPerPage
= 0;
1250 else // might need scrolling
1252 // Round up integer division to catch any "leftover" client space.
1253 scrollUnits
= (virtSize
+ pixelsPerUnit
- 1) / pixelsPerUnit
;
1255 // Calculate the number of fully scroll units
1256 scrollLinesPerPage
= clientSize
/ pixelsPerUnit
;
1258 if ( scrollLinesPerPage
>= scrollUnits
)
1260 // we're big enough to not need scrolling
1263 scrollLinesPerPage
= 0;
1265 else // we do need a scrollbar
1267 if ( scrollLinesPerPage
< 1 )
1268 scrollLinesPerPage
= 1;
1270 // Correct position if greater than extent of canvas minus
1271 // the visible portion of it or if below zero
1272 const int posMax
= scrollUnits
- scrollLinesPerPage
;
1273 if ( scrollPosition
> posMax
)
1274 scrollPosition
= posMax
;
1275 else if ( scrollPosition
< 0 )
1280 // in wxSHOW_SB_NEVER case don't show the scrollbar even if it's needed, in
1281 // wxSHOW_SB_ALWAYS case show the scrollbar even if it's not needed by
1282 // passing a special range value to SetScrollbar()
1284 switch ( visibility
)
1286 case wxSHOW_SB_NEVER
:
1290 case wxSHOW_SB_ALWAYS
:
1291 range
= scrollUnits
? scrollUnits
: -1;
1295 wxFAIL_MSG( wxS("unknown scrollbar visibility") );
1298 case wxSHOW_SB_DEFAULT
:
1299 range
= scrollUnits
;
1304 m_win
->SetScrollbar(orient
, scrollPosition
, scrollLinesPerPage
, range
);
1307 void wxScrollHelper::AdjustScrollbars()
1309 static wxRecursionGuardFlag s_flagReentrancy
;
1310 wxRecursionGuard
guard(s_flagReentrancy
);
1311 if ( guard
.IsInside() )
1313 // don't reenter AdjustScrollbars() while another call to
1314 // AdjustScrollbars() is in progress because this may lead to calling
1315 // ScrollWindow() twice and this can really happen under MSW if
1316 // SetScrollbar() call below adds or removes the scrollbar which
1317 // changes the window size and hence results in another
1318 // AdjustScrollbars() call
1322 int oldXScroll
= m_xScrollPosition
;
1323 int oldYScroll
= m_yScrollPosition
;
1325 // we may need to readjust the scrollbars several times as enabling one of
1326 // them reduces the area available for the window contents and so can make
1327 // the other scrollbar necessary now although it wasn't necessary before
1329 // VZ: normally this loop should be over in at most 2 iterations, I don't
1330 // know why do we need 5 of them
1331 for ( int iterationCount
= 0; iterationCount
< 5; iterationCount
++ )
1333 wxSize clientSize
= GetTargetSize();
1334 const wxSize virtSize
= m_targetWindow
->GetVirtualSize();
1336 // this block of code tries to work around the following problem: the
1337 // window could have been just resized to have enough space to show its
1338 // full contents without the scrollbars, but its client size could be
1339 // not big enough because it does have the scrollbars right now and so
1340 // the scrollbars would remain even though we don't need them any more
1342 // to prevent this from happening, check if we have enough space for
1343 // everything without the scrollbars and explicitly disable them then
1344 const wxSize availSize
= GetSizeAvailableForScrollTarget(
1345 m_win
->GetSize() - m_win
->GetWindowBorderSize());
1346 if ( availSize
!= clientSize
)
1348 if ( availSize
.x
>= virtSize
.x
&& availSize
.y
>= virtSize
.y
)
1350 // this will be enough to make the scrollbars disappear below
1351 // and then the client size will indeed become equal to the
1352 // full available size
1353 clientSize
= availSize
;
1358 DoAdjustScrollbar(wxHORIZONTAL
,
1361 m_xScrollPixelsPerLine
,
1364 m_xScrollLinesPerPage
,
1367 DoAdjustScrollbar(wxVERTICAL
,
1370 m_yScrollPixelsPerLine
,
1373 m_yScrollLinesPerPage
,
1377 // If a scrollbar (dis)appeared as a result of this, we need to adjust
1378 // them again but if the client size didn't change, then we're done
1379 if ( GetTargetSize() == clientSize
)
1384 // Sorry, some Motif-specific code to implement a backing pixmap
1385 // for the wxRETAINED style. Implementing a backing store can't
1386 // be entirely generic because it relies on the wxWindowDC implementation
1387 // to duplicate X drawing calls for the backing pixmap.
1389 if ( m_targetWindow
->GetWindowStyle() & wxRETAINED
)
1391 Display
* dpy
= XtDisplay((Widget
)m_targetWindow
->GetMainWidget());
1393 int totalPixelWidth
= m_xScrollLines
* m_xScrollPixelsPerLine
;
1394 int totalPixelHeight
= m_yScrollLines
* m_yScrollPixelsPerLine
;
1395 if (m_targetWindow
->GetBackingPixmap() &&
1396 !((m_targetWindow
->GetPixmapWidth() == totalPixelWidth
) &&
1397 (m_targetWindow
->GetPixmapHeight() == totalPixelHeight
)))
1399 XFreePixmap (dpy
, (Pixmap
) m_targetWindow
->GetBackingPixmap());
1400 m_targetWindow
->SetBackingPixmap((WXPixmap
) 0);
1403 if (!m_targetWindow
->GetBackingPixmap() &&
1404 (m_xScrollLines
!= 0) && (m_yScrollLines
!= 0))
1406 int depth
= wxDisplayDepth();
1407 m_targetWindow
->SetPixmapWidth(totalPixelWidth
);
1408 m_targetWindow
->SetPixmapHeight(totalPixelHeight
);
1409 m_targetWindow
->SetBackingPixmap((WXPixmap
) XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
1410 m_targetWindow
->GetPixmapWidth(), m_targetWindow
->GetPixmapHeight(), depth
));
1416 if (oldXScroll
!= m_xScrollPosition
)
1418 if (m_xScrollingEnabled
)
1419 m_targetWindow
->ScrollWindow( m_xScrollPixelsPerLine
* (oldXScroll
- m_xScrollPosition
), 0,
1422 m_targetWindow
->Refresh(true, GetScrollRect());
1425 if (oldYScroll
!= m_yScrollPosition
)
1427 if (m_yScrollingEnabled
)
1428 m_targetWindow
->ScrollWindow( 0, m_yScrollPixelsPerLine
* (oldYScroll
-m_yScrollPosition
),
1431 m_targetWindow
->Refresh(true, GetScrollRect());
1435 void wxScrollHelper::DoScroll( int x_pos
, int y_pos
)
1437 if (!m_targetWindow
)
1440 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
1441 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
1444 GetTargetSize(&w
, &h
);
1446 // compute new position:
1447 int new_x
= m_xScrollPosition
;
1448 int new_y
= m_yScrollPosition
;
1450 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
1454 // Calculate page size i.e. number of scroll units you get on the
1455 // current client window
1456 int noPagePositions
= w
/m_xScrollPixelsPerLine
;
1457 if (noPagePositions
< 1) noPagePositions
= 1;
1459 // Correct position if greater than extent of canvas minus
1460 // the visible portion of it or if below zero
1461 new_x
= wxMin( m_xScrollLines
-noPagePositions
, new_x
);
1462 new_x
= wxMax( 0, new_x
);
1464 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
1468 // Calculate page size i.e. number of scroll units you get on the
1469 // current client window
1470 int noPagePositions
= h
/m_yScrollPixelsPerLine
;
1471 if (noPagePositions
< 1) noPagePositions
= 1;
1473 // Correct position if greater than extent of canvas minus
1474 // the visible portion of it or if below zero
1475 new_y
= wxMin( m_yScrollLines
-noPagePositions
, new_y
);
1476 new_y
= wxMax( 0, new_y
);
1479 if ( new_x
== m_xScrollPosition
&& new_y
== m_yScrollPosition
)
1480 return; // nothing to do, the position didn't change
1482 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
1483 // otherwise invalidated area could be updated incorrectly later when
1484 // ScrollWindow() makes sure they're repainted before scrolling them
1485 m_targetWindow
->Update();
1487 // update the position and scroll the window now:
1488 if (m_xScrollPosition
!= new_x
)
1490 int old_x
= m_xScrollPosition
;
1491 m_xScrollPosition
= new_x
;
1492 m_win
->SetScrollPos( wxHORIZONTAL
, new_x
);
1493 m_targetWindow
->ScrollWindow( (old_x
-new_x
)*m_xScrollPixelsPerLine
, 0,
1497 if (m_yScrollPosition
!= new_y
)
1499 int old_y
= m_yScrollPosition
;
1500 m_yScrollPosition
= new_y
;
1501 m_win
->SetScrollPos( wxVERTICAL
, new_y
);
1502 m_targetWindow
->ScrollWindow( 0, (old_y
-new_y
)*m_yScrollPixelsPerLine
,
1507 #endif // wxHAS_GENERIC_SCROLLWIN
1509 // ----------------------------------------------------------------------------
1510 // wxScrolled<T> and wxScrolledWindow implementation
1511 // ----------------------------------------------------------------------------
1513 wxSize
wxScrolledT_Helper::FilterBestSize(const wxWindow
*win
,
1514 const wxScrollHelper
*helper
,
1515 const wxSize
& origBest
)
1517 // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1518 // all scrollable windows should behave like this, only those that
1519 // contain children controls within scrollable area
1520 // (i.e., wxScrolledWindow) and other some scrollable windows may
1521 // have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1523 wxSize best
= origBest
;
1525 if ( win
->GetAutoLayout() )
1527 // Only use the content to set the window size in the direction
1528 // where there's no scrolling; otherwise we're going to get a huge
1529 // window in the direction in which scrolling is enabled
1531 helper
->GetScrollPixelsPerUnit(&ppuX
, &ppuY
);
1533 // NB: This code used to use *current* size if min size wasn't
1534 // specified, presumably to get some reasonable (i.e., larger than
1535 // minimal) size. But that's a wrong thing to do in GetBestSize(),
1536 // so we use minimal size as specified. If the app needs some
1537 // minimal size for its scrolled window, it should set it and put
1538 // the window into sizer as expandable so that it can use all space
1541 // See also http://svn.wxwidgets.org/viewvc/wx?view=rev&revision=45864
1543 wxSize minSize
= win
->GetMinSize();
1546 best
.x
= minSize
.x
+ wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1549 best
.y
= minSize
.y
+ wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
1556 WXLRESULT
wxScrolledT_Helper::FilterMSWWindowProc(WXUINT nMsg
, WXLRESULT rc
)
1559 // we need to process arrows ourselves for scrolling
1560 if ( nMsg
== WM_GETDLGCODE
)
1562 rc
|= DLGC_WANTARROWS
;
1569 // NB: skipping wxScrolled<T> in wxRTTI information because being a templte,
1570 // it doesn't and can't implement wxRTTI support
1571 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)