// them to wxScrollHelper
// ----------------------------------------------------------------------------
-class wxScrollHelperEvtHandler : public wxEvtHandler
+class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler
{
public:
wxScrollHelperEvtHandler(wxScrollHelper *scrollHelper)
virtual bool ProcessEvent(wxEvent& event);
+ void ResetDrawnFlag() { m_hasDrawnWindow = FALSE; }
+
private:
wxScrollHelper *m_scrollHelper;
+
+ bool m_hasDrawnWindow;
};
// ----------------------------------------------------------------------------
{
wxEventType evType = event.GetEventType();
+ // the explanation of wxEVT_PAINT processing hack: for historic reasons
+ // there are 2 ways to process this event in classes deriving from
+ // wxScrolledWindow. The user code may
+ //
+ // 1. override wxScrolledWindow::OnDraw(dc)
+ // 2. define its own OnPaint() handler
+ //
+ // In addition, in wxUniversal wxWindow defines OnPaint() itself and
+ // always processes the draw event, so we can't just try the window
+ // OnPaint() first and call our HandleOnPaint() if it doesn't process it
+ // (the latter would never be called in wxUniversal).
+ //
+ // So the solution is to have a flag telling us whether the user code drew
+ // anything in the window. We set it to true here but reset it to false in
+ // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
+ // user code defined OnPaint() in the derived class)
+ m_hasDrawnWindow = TRUE;
+
+ // pass it on to the real handler
+ bool processed = wxEvtHandler::ProcessEvent(event);
+
+ // always process the size events ourselves, even if the user code handles
+ // them as well, as we need to AdjustScrollbars()
+ //
+ // NB: it is important to do it after processing the event in the normal
+ // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
+ // scrollbar[s] (dis)appear and it should be seen by the user code
+ // after this one
if ( evType == wxEVT_SIZE )
{
m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
- }
- if ( wxEvtHandler::ProcessEvent(event) )
return TRUE;
+ }
+
+ if ( processed )
+ {
+ // normally, nothing more to do here - except if it was a paint event
+ // which wasn't really processed, then we'll try to call our
+ // OnDraw() below (from HandleOnPaint)
+ if ( m_hasDrawnWindow )
+ {
+ return TRUE;
+ }
+ }
// reset the skipped flag to FALSE as it might have been set to TRUE in
// ProcessEvent() above
m_timerAutoScroll = (wxTimer *)NULL;
+ m_handler = NULL;
+
if ( win )
SetWindow(win);
}
-void wxScrollHelper::SetWindow(wxWindow *win)
-{
- wxCHECK_RET( win, _T("wxScrollHelper needs a window to scroll") );
-
- m_targetWindow = m_win = win;
-
- // install the event handler which will intercept the events we're
- // interested in
- m_win->PushEventHandler(new wxScrollHelperEvtHandler(this));
-}
-
wxScrollHelper::~wxScrollHelper()
{
StopAutoScrolling();
- if ( m_targetWindow )
- m_targetWindow->PopEventHandler(TRUE /* do delete it */);
- if ( m_win && m_win != m_targetWindow)
- m_win->PopEventHandler(TRUE /* do delete it */);
+ DeleteEvtHandler();
}
// ----------------------------------------------------------------------------
}
// ----------------------------------------------------------------------------
-// target window handling
+// [target] window handling
// ----------------------------------------------------------------------------
-void wxScrollHelper::SetTargetWindow( wxWindow *target )
+void wxScrollHelper::DeleteEvtHandler()
+{
+ // search for m_handler in the handler list
+ if ( m_win && m_handler )
+ {
+ wxEvtHandler *handlerPrev = NULL,
+ *handler = m_win->GetEventHandler();
+ while ( handler )
+ {
+ if ( handler == m_handler )
+ {
+ wxEvtHandler *handlerNext = handler->GetNextHandler();
+ if ( handlerPrev )
+ {
+ handlerPrev->SetNextHandler(handlerNext);
+ }
+ else
+ {
+ m_win->SetEventHandler(handlerNext);
+ }
+
+ handler->SetNextHandler(NULL);
+ delete handler;
+ m_handler = NULL;
+
+ return;
+ }
+
+ handlerPrev = handler;
+ handler = handler->GetNextHandler();
+ }
+
+ wxFAIL_MSG( _T("where has our event handler gone?") );
+ }
+}
+
+void wxScrollHelper::SetWindow(wxWindow *win)
+{
+ wxCHECK_RET( win, _T("wxScrollHelper needs a window to scroll") );
+
+ m_win = win;
+
+ // by default, the associated window is also the target window
+ DoSetTargetWindow(win);
+}
+
+void wxScrollHelper::DoSetTargetWindow(wxWindow *target)
{
- wxASSERT_MSG( target, wxT("target window must not be NULL") );
- // FIXME: There is a potential problem with this way of deleting
- // event handlers, basically you can not be sure that you delete
- // the event handler that was create by this wxScrollHelper.
- // Remove the old event handler from the previous target scroll window.
- if (m_targetWindow && m_targetWindow != m_win)
- m_targetWindow->PopEventHandler(TRUE /* Delete old event handler*/);
m_targetWindow = target;
- // Install a new event handler, which will intercept the events we're
- // interested in from the target scroll window.
- if (m_targetWindow != m_win)
- m_targetWindow->PushEventHandler(new wxScrollHelperEvtHandler(this));
+
+ // install the event handler which will intercept the events we're
+ // interested in (but only do it for our real window, not the target window
+ // which we scroll - we don't need to hijack its events)
+ if ( m_targetWindow == m_win )
+ {
+ // if we already have a handler, delete it first
+ DeleteEvtHandler();
+
+ m_handler = new wxScrollHelperEvtHandler(this);
+ m_targetWindow->PushEventHandler(m_handler);
+ }
+}
+
+void wxScrollHelper::SetTargetWindow(wxWindow *target)
+{
+ wxCHECK_RET( target, wxT("target window must not be NULL") );
+
+ if ( target == m_targetWindow )
+ return;
+
+ DoSetTargetWindow(target);
}
wxWindow *wxScrollHelper::GetTargetWindow() const
if (orient == wxHORIZONTAL)
{
m_xScrollPosition += nScrollInc;
- m_targetWindow->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
+ m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
}
else
{
m_yScrollPosition += nScrollInc;
- m_targetWindow->SetScrollPos(wxVERTICAL, m_yScrollPosition);
+ m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
}
bool needsRefresh = FALSE;
m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
m_xScrollPosition = wxMax( 0, m_xScrollPosition );
- m_targetWindow->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
+ m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
// The amount by which we scroll when paging
SetScrollPageSize(wxHORIZONTAL, noPagePositions);
}
else
{
m_xScrollPosition = 0;
- m_targetWindow->SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
+ m_win->SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
}
if (m_yScrollLines > 0)
m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
m_yScrollPosition = wxMax( 0, m_yScrollPosition );
- m_targetWindow->SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
+ m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
// The amount by which we scroll when paging
SetScrollPageSize(wxVERTICAL, noPagePositions);
}
else
{
m_yScrollPosition = 0;
- m_targetWindow->SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
+ m_win->SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
}
if (oldXScroll != m_xScrollPosition)
m_xScrollPosition = wxMax( 0, m_xScrollPosition );
if (old_x != m_xScrollPosition) {
- m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
+ m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0,
GetRect() );
}
m_yScrollPosition = wxMax( 0, m_yScrollPosition );
if (old_y != m_yScrollPosition) {
- m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition );
+ m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine,
GetRect() );
}
void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
{
#if wxUSE_CONSTRAINTS
- if ( m_targetWindow->GetAutoLayout() )
- m_targetWindow->Layout();
+ if ( m_win->GetAutoLayout() )
+ m_win->Layout();
#endif
AdjustScrollbars();
// scroll position
void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
{
- wxPaintDC dc(m_targetWindow);
+ // don't use m_targetWindow here, this is always called for ourselves
+ wxPaintDC dc(m_win);
DoPrepareDC(dc);
OnDraw(dc);
IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
+BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel)
+ EVT_PAINT(wxGenericScrolledWindow::OnPaint)
+END_EVENT_TABLE()
+
bool wxGenericScrolledWindow::Create(wxWindow *parent,
wxWindowID id,
const wxPoint& pos,
{
}
+void wxGenericScrolledWindow::OnPaint(wxPaintEvent& event)
+{
+ // the user code didn't really draw the window if we got here, so set this
+ // flag to try to call OnDraw() later
+ m_handler->ResetDrawnFlag();
+
+ event.Skip();
+}
+
#if WXWIN_COMPATIBILITY
+
void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
{
*x_page = GetScrollPageSize(wxHORIZONTAL);
if ( yy )
*yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
}
+
#endif // WXWIN_COMPATIBILITY
#endif // !wxGTK