X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3a8c693aea7d83397af67825943e2750f1723628..e1910715af0629ad65976e87cdeca23ede2fd6ee:/src/generic/scrlwing.cpp diff --git a/src/generic/scrlwing.cpp b/src/generic/scrlwing.cpp index 3897dca59f..b19d93d85b 100644 --- a/src/generic/scrlwing.cpp +++ b/src/generic/scrlwing.cpp @@ -40,6 +40,7 @@ #include "wx/scrolwin.h" #include "wx/panel.h" #include "wx/timer.h" +#include "wx/sizer.h" #ifdef __WXMSW__ #include // for DLGC_WANTARROWS @@ -57,16 +58,14 @@ #endif #endif -#ifndef __WXGTK__ IMPLEMENT_CLASS(wxScrolledWindow, wxGenericScrolledWindow) -#endif // ---------------------------------------------------------------------------- // wxScrollHelperEvtHandler: intercept the events from the window and forward // them to wxScrollHelper // ---------------------------------------------------------------------------- -class wxScrollHelperEvtHandler : public wxEvtHandler +class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler { public: wxScrollHelperEvtHandler(wxScrollHelper *scrollHelper) @@ -76,8 +75,12 @@ public: virtual bool ProcessEvent(wxEvent& event); + void ResetDrawnFlag() { m_hasDrawnWindow = FALSE; } + private: wxScrollHelper *m_scrollHelper; + + bool m_hasDrawnWindow; }; // ---------------------------------------------------------------------------- @@ -169,15 +172,58 @@ void wxAutoScrollTimer::Notify() bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event) { - if ( wxEvtHandler::ProcessEvent(event) ) + 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); + 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 event.Skip(FALSE); - wxEventType evType = event.GetEventType(); - if ( evType == wxEVT_PAINT ) { m_scrollHelper->HandleOnPaint((wxPaintEvent &)event); @@ -211,10 +257,6 @@ bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event) m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event); } #endif // wxUSE_MOUSEWHEEL - else if ( evType == wxEVT_SIZE ) - { - m_scrollHelper->HandleOnSize((wxSizeEvent &)event); - } else if ( evType == wxEVT_CHAR ) { m_scrollHelper->HandleOnChar((wxKeyEvent &)event); @@ -253,27 +295,17 @@ wxScrollHelper::wxScrollHelper(wxWindow *win) 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 */); + DeleteEvtHandler(); } // ---------------------------------------------------------------------------- @@ -321,22 +353,22 @@ void wxScrollHelper::SetScrollbars(int pixelsPerUnitX, int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine; int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine; - if (m_targetWindow->m_backingPixmap && - !((m_targetWindow->m_pixmapWidth == totalPixelWidth) && - (m_targetWindow->m_pixmapHeight == totalPixelHeight))) + if (m_targetWindow->GetBackingPixmap() && + !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) && + (m_targetWindow->GetPixmapHeight() == totalPixelHeight))) { - XFreePixmap (dpy, (Pixmap) m_targetWindow->m_backingPixmap); - m_targetWindow->m_backingPixmap = (WXPixmap) 0; + XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap()); + m_targetWindow->SetBackingPixmap((WXPixmap) 0); } - if (!m_targetWindow->m_backingPixmap && + if (!m_targetWindow->GetBackingPixmap() && (noUnitsX != 0) && (noUnitsY != 0)) { int depth = wxDisplayDepth(); - m_pixmapWidth = totalPixelWidth; - m_pixmapHeight = totalPixelHeight; - m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)), - m_pixmapWidth, m_pixmapHeight, depth); + m_targetWindow->SetPixmapWidth(totalPixelWidth); + m_targetWindow->SetPixmapHeight(totalPixelHeight); + m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)), + m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth)); } } @@ -353,13 +385,60 @@ void wxScrollHelper::SetScrollbars(int pixelsPerUnitX, } // ---------------------------------------------------------------------------- -// 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 ) + { + if ( m_win->RemoveEventHandler(m_handler) ) + { + delete m_handler; + } + //else: something is very wrong, so better [maybe] leak memory than + // risk a crash because of double deletion + + m_handler = NULL; + } +} + +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") ); m_targetWindow = target; + + // 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 @@ -386,12 +465,12 @@ void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event) if (orient == wxHORIZONTAL) { m_xScrollPosition += nScrollInc; - m_targetWindow->SetScrollPos(wxHORIZONTAL, m_xScrollPosition, FALSE); + m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition); } else { m_yScrollPosition += nScrollInc; - m_targetWindow->SetScrollPos(wxVERTICAL, m_yScrollPosition, FALSE); + m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition); } bool needsRefresh = FALSE; @@ -556,14 +635,14 @@ void wxScrollHelper::AdjustScrollbars() 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) @@ -580,14 +659,14 @@ void wxScrollHelper::AdjustScrollbars() 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) @@ -679,7 +758,7 @@ void wxScrollHelper::Scroll( int x_pos, int y_pos ) m_xScrollPosition = wxMax( 0, m_xScrollPosition ); if (old_x != m_xScrollPosition) { - m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, FALSE ); + m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition ); m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0, GetRect() ); } @@ -700,7 +779,7 @@ void wxScrollHelper::Scroll( int x_pos, int y_pos ) m_yScrollPosition = wxMax( 0, m_yScrollPosition ); if (old_y != m_yScrollPosition) { - m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, FALSE ); + m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition ); m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine, GetRect() ); } @@ -735,7 +814,7 @@ void wxScrollHelper::GetViewStart (int *x, int *y) const *y = m_yScrollPosition; } -void wxScrollHelper::CalcScrolledPosition(int x, int y, int *xx, int *yy) const +void wxScrollHelper::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const { if ( xx ) *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine; @@ -743,7 +822,7 @@ void wxScrollHelper::CalcScrolledPosition(int x, int y, int *xx, int *yy) const *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine; } -void wxScrollHelper::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const +void wxScrollHelper::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const { if ( xx ) *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine; @@ -758,11 +837,6 @@ void wxScrollHelper::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) cons // Default OnSize resets scrollbars, if any void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event)) { -#if wxUSE_CONSTRAINTS - if ( m_targetWindow->GetAutoLayout() ) - m_targetWindow->Layout(); -#endif - AdjustScrollbars(); } @@ -770,7 +844,8 @@ void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event)) // 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); @@ -811,6 +886,9 @@ void wxScrollHelper::HandleOnChar(wxKeyEvent& event) szy = -1; } + int xScrollOld = m_xScrollPosition, + yScrollOld = m_yScrollPosition; + int dsty; switch ( event.KeyCode() ) { @@ -853,6 +931,22 @@ void wxScrollHelper::HandleOnChar(wxKeyEvent& event) // not for us event.Skip(); } + + if ( m_xScrollPosition != xScrollOld ) + { + wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition, + wxHORIZONTAL); + event.SetEventObject(m_win); + m_win->GetEventHandler()->ProcessEvent(event); + } + + if ( m_yScrollPosition != yScrollOld ) + { + wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition, + wxVERTICAL); + event.SetEventObject(m_win); + m_win->GetEventHandler()->ProcessEvent(event); + } } // ---------------------------------------------------------------------------- @@ -960,9 +1054,24 @@ void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event) { lines *= event.GetLinesPerAction(); - int vsx, vsy; - GetViewStart(&vsx, &vsy); - Scroll(-1, vsy - lines); + wxScrollWinEvent newEvent; + + newEvent.SetPosition(0); + newEvent.SetOrientation(wxVERTICAL); + newEvent.m_eventObject = m_win; + if (lines > 0) + newEvent.m_eventType = wxEVT_SCROLLWIN_LINEUP; + else + newEvent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN; + + int times = abs(lines); + for (; times > 0; times --) + m_win->GetEventHandler()->ProcessEvent(newEvent); + + /* Old Way */ + // int vsx, vsy; + // GetViewStart(&vsx, &vsy); + // Scroll(-1, vsy - lines); } } @@ -974,6 +1083,10 @@ void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event) 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, @@ -985,11 +1098,6 @@ bool wxGenericScrolledWindow::Create(wxWindow *parent, bool ok = wxPanel::Create(parent, id, pos, size, style, name); -#ifdef __WXMSW__ - // we need to process arrows ourselves for scrolling - m_lDlgCode |= DLGC_WANTARROWS; -#endif // __WXMSW__ - return ok; } @@ -997,7 +1105,52 @@ wxGenericScrolledWindow::~wxGenericScrolledWindow() { } +bool wxGenericScrolledWindow::Layout() +{ + if (GetSizer()) + { + // Take into account the virtual size and scrolled position of the window + int x, y, w, h; + CalcScrolledPosition(0,0, &x,&y); + GetVirtualSize(&w, &h); + GetSizer()->SetDimension(x, y, w, h); + return TRUE; + } + + // fall back to default for LayoutConstraints + return wxPanel::Layout(); +} + +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(); +} + +#ifdef __WXMSW__ +long +wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg, + WXWPARAM wParam, + WXLPARAM lParam) +{ + long rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam); + + // we need to process arrows ourselves for scrolling + if ( nMsg == WM_GETDLGCODE ) + { + rc |= DLGC_WANTARROWS; + } + + return rc; +} + +#endif // __WXMSW__ + #if WXWIN_COMPATIBILITY + void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const { *x_page = GetScrollPageSize(wxHORIZONTAL); @@ -1011,6 +1164,7 @@ void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, fl if ( yy ) *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine); } + #endif // WXWIN_COMPATIBILITY #endif // !wxGTK