Make scrollbar private to wxWindow under wxQt

Use QScrollbar directly instead of wxScrollbar for the window scrollbars
to ensure that wxWindow::GetChildren() doesn't return these scrollbars.

Closes https://github.com/wxWidgets/wxWidgets/pull/1124
This commit is contained in:
Graham Dawes
2019-01-08 11:48:45 +00:00
committed by Vadim Zeitlin
parent 34ecc6efc4
commit aa422c6be2
2 changed files with 97 additions and 76 deletions

View File

@@ -15,9 +15,8 @@ class QShortcut;
template < class T > class QList; template < class T > class QList;
class QWidget; class QWidget;
class QScrollWindow;
class QAbstractScrollArea;
class QScrollArea; class QScrollArea;
class QScrollBar;
class QPicture; class QPicture;
class QPainter; class QPainter;
@@ -215,12 +214,11 @@ private:
void Init(); void Init();
QScrollArea *m_qtContainer; QScrollArea *m_qtContainer;
wxScrollBar *m_horzScrollBar; QScrollBar *m_horzScrollBar;
wxScrollBar *m_vertScrollBar; QScrollBar *m_vertScrollBar;
void QtOnScrollBarEvent( wxScrollEvent& event );
QScrollBar *QtGetScrollBar( int orientation ) const;
wxScrollBar *QtGetScrollBar( int orientation ) const; QScrollBar *QtSetScrollBar( int orientation, QScrollBar *scrollBar=NULL );
wxScrollBar *QtSetScrollBar( int orientation, wxScrollBar *scrollBar=NULL );
bool QtSetBackgroundStyle(); bool QtSetBackgroundStyle();

View File

@@ -14,6 +14,7 @@
#include <QtGui/QPicture> #include <QtGui/QPicture>
#include <QtGui/QPainter> #include <QtGui/QPainter>
#include <QtWidgets/QScrollBar>
#include <QtWidgets/QGridLayout> #include <QtWidgets/QGridLayout>
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <QtWidgets/QWidget> #include <QtWidgets/QWidget>
@@ -68,6 +69,69 @@ wxQtScrollArea::wxQtScrollArea( wxWindowQt *parent, wxWindowQt *handler )
{ {
} }
class wxQtInternalScrollBar : public wxQtEventSignalHandler< QScrollBar, wxWindowQt >
{
public:
wxQtInternalScrollBar(wxWindowQt *parent, wxWindowQt *handler );
void actionTriggered( int action );
void sliderReleased();
void valueChanged( int position );
};
wxQtInternalScrollBar::wxQtInternalScrollBar( wxWindowQt *parent, wxWindowQt *handler )
: wxQtEventSignalHandler< QScrollBar, wxWindowQt >( parent, handler )
{
connect( this, &QScrollBar::actionTriggered, this, &wxQtInternalScrollBar::actionTriggered );
connect( this, &QScrollBar::sliderReleased, this, &wxQtInternalScrollBar::sliderReleased );
}
void wxQtInternalScrollBar::actionTriggered( int action )
{
wxEventType eventType = wxEVT_NULL;
switch( action )
{
case QAbstractSlider::SliderSingleStepAdd:
eventType = wxEVT_SCROLLWIN_LINEDOWN;
break;
case QAbstractSlider::SliderSingleStepSub:
eventType = wxEVT_SCROLLWIN_LINEUP;
break;
case QAbstractSlider::SliderPageStepAdd:
eventType = wxEVT_SCROLLWIN_PAGEDOWN;
break;
case QAbstractSlider::SliderPageStepSub:
eventType = wxEVT_SCROLLWIN_PAGEUP;
break;
case QAbstractSlider::SliderToMinimum:
eventType = wxEVT_SCROLLWIN_TOP;
break;
case QAbstractSlider::SliderToMaximum:
eventType = wxEVT_SCROLLWIN_BOTTOM;
break;
case QAbstractSlider::SliderMove:
eventType = wxEVT_SCROLLWIN_THUMBTRACK;
break;
default:
return;
}
if ( GetHandler() )
{
wxScrollWinEvent e( eventType, sliderPosition(), wxQtConvertOrientation( orientation() ) );
EmitEvent( e );
}
}
void wxQtInternalScrollBar::sliderReleased()
{
if ( GetHandler() )
{
wxScrollWinEvent e( wxEVT_SCROLLWIN_THUMBRELEASE, sliderPosition(), wxQtConvertOrientation( orientation() ) );
EmitEvent( e );
}
}
#if wxUSE_ACCEL || defined( Q_MOC_RUN ) #if wxUSE_ACCEL || defined( Q_MOC_RUN )
class wxQtShortcutHandler : public QObject, public wxQtSignalHandler< wxWindowQt > class wxQtShortcutHandler : public QObject, public wxQtSignalHandler< wxWindowQt >
{ {
@@ -490,11 +554,9 @@ void wxWindowQt::DoGetTextExtent(const wxString& string, int *x, int *y, int *de
/* Returns a scrollbar for the given orientation, or NULL if the scrollbar /* Returns a scrollbar for the given orientation, or NULL if the scrollbar
* has not been previously created and create is false */ * has not been previously created and create is false */
wxScrollBar *wxWindowQt::QtGetScrollBar( int orientation ) const QScrollBar *wxWindowQt::QtGetScrollBar( int orientation ) const
{ {
wxCHECK_MSG( CanScroll( orientation ), NULL, "Window can't scroll in that orientation" ); QScrollBar *scrollBar = NULL;
wxScrollBar *scrollBar = NULL;
if ( orientation == wxHORIZONTAL ) if ( orientation == wxHORIZONTAL )
scrollBar = m_horzScrollBar; scrollBar = m_horzScrollBar;
@@ -506,7 +568,7 @@ wxScrollBar *wxWindowQt::QtGetScrollBar( int orientation ) const
/* Returns a new scrollbar for the given orientation, or set the scrollbar /* Returns a new scrollbar for the given orientation, or set the scrollbar
* passed as parameter */ * passed as parameter */
wxScrollBar *wxWindowQt::QtSetScrollBar( int orientation, wxScrollBar *scrollBar ) QScrollBar *wxWindowQt::QtSetScrollBar( int orientation, QScrollBar *scrollBar )
{ {
QScrollArea *scrollArea = QtGetScrollBarsContainer(); QScrollArea *scrollArea = QtGetScrollBarsContainer();
wxCHECK_MSG( scrollArea, NULL, "Window without scrolling area" ); wxCHECK_MSG( scrollArea, NULL, "Window without scrolling area" );
@@ -514,30 +576,19 @@ wxScrollBar *wxWindowQt::QtSetScrollBar( int orientation, wxScrollBar *scrollBar
// Create a new scrollbar if needed // Create a new scrollbar if needed
if ( !scrollBar ) if ( !scrollBar )
{ {
scrollBar = new wxScrollBar( const_cast< wxWindowQt* >( this ), wxID_ANY, scrollBar = new wxQtInternalScrollBar(this, this);
wxDefaultPosition, wxDefaultSize, scrollBar->setOrientation( orientation == wxHORIZONTAL ? Qt::Horizontal : Qt::Vertical );
orientation == wxHORIZONTAL ? wxSB_HORIZONTAL : wxSB_VERTICAL);
// Connect scrollbar events to this window
scrollBar->Bind( wxEVT_SCROLL_LINEUP, &wxWindowQt::QtOnScrollBarEvent, this );
scrollBar->Bind( wxEVT_SCROLL_LINEDOWN, &wxWindowQt::QtOnScrollBarEvent, this );
scrollBar->Bind( wxEVT_SCROLL_PAGEUP, &wxWindowQt::QtOnScrollBarEvent, this );
scrollBar->Bind( wxEVT_SCROLL_PAGEDOWN, &wxWindowQt::QtOnScrollBarEvent, this );
scrollBar->Bind( wxEVT_SCROLL_TOP, &wxWindowQt::QtOnScrollBarEvent, this );
scrollBar->Bind( wxEVT_SCROLL_BOTTOM, &wxWindowQt::QtOnScrollBarEvent, this );
scrollBar->Bind( wxEVT_SCROLL_THUMBTRACK, &wxWindowQt::QtOnScrollBarEvent, this );
scrollBar->Bind( wxEVT_SCROLL_THUMBRELEASE, &wxWindowQt::QtOnScrollBarEvent, this );
} }
// Let Qt handle layout // Let Qt handle layout
if ( orientation == wxHORIZONTAL ) if ( orientation == wxHORIZONTAL )
{ {
scrollArea->setHorizontalScrollBar( scrollBar->GetQScrollBar() ); scrollArea->setHorizontalScrollBar( scrollBar );
m_horzScrollBar = scrollBar; m_horzScrollBar = scrollBar;
} }
else else
{ {
scrollArea->setVerticalScrollBar( scrollBar->GetQScrollBar() ); scrollArea->setVerticalScrollBar( scrollBar );
m_vertScrollBar = scrollBar; m_vertScrollBar = scrollBar;
} }
return scrollBar; return scrollBar;
@@ -546,10 +597,8 @@ wxScrollBar *wxWindowQt::QtSetScrollBar( int orientation, wxScrollBar *scrollBar
void wxWindowQt::SetScrollbar( int orientation, int pos, int thumbvisible, int range, bool refresh ) void wxWindowQt::SetScrollbar( int orientation, int pos, int thumbvisible, int range, bool refresh )
{ {
wxCHECK_RET( CanScroll( orientation ), "Window can't scroll in that orientation" );
//If not exist, create the scrollbar //If not exist, create the scrollbar
wxScrollBar *scrollBar = QtGetScrollBar( orientation ); QScrollBar *scrollBar = QtGetScrollBar( orientation );
if ( scrollBar == NULL ) if ( scrollBar == NULL )
scrollBar = QtSetScrollBar( orientation ); scrollBar = QtSetScrollBar( orientation );
@@ -557,79 +606,53 @@ void wxWindowQt::SetScrollbar( int orientation, int pos, int thumbvisible, int r
// scrollBar == NULL and it is not a problem // scrollBar == NULL and it is not a problem
if ( scrollBar ) if ( scrollBar )
{ {
scrollBar->SetScrollbar( pos, thumbvisible, range, thumbvisible, refresh ); scrollBar->setRange( 0, range - thumbvisible );
if ( HasFlag( wxALWAYS_SHOW_SB ) && ( range == 0 ) ) scrollBar->setPageStep( thumbvisible );
scrollBar->blockSignals( true );
scrollBar->setValue(pos);
scrollBar->blockSignals( false );
scrollBar->show();
if ( HasFlag(wxALWAYS_SHOW_SB) && (range == 0) )
{ {
// Disable instead of hide // Disable instead of hide
scrollBar->GetHandle()->show(); scrollBar->setEnabled( false );
scrollBar->GetHandle()->setEnabled( false );
} }
else else
scrollBar->GetHandle()->setEnabled( true ); scrollBar->setEnabled( true );
} }
} }
void wxWindowQt::SetScrollPos( int orientation, int pos, bool WXUNUSED( refresh )) void wxWindowQt::SetScrollPos( int orientation, int pos, bool WXUNUSED( refresh ))
{ {
wxScrollBar *scrollBar = QtGetScrollBar( orientation ); QScrollBar *scrollBar = QtGetScrollBar( orientation );
if ( scrollBar ) if ( scrollBar )
scrollBar->SetThumbPosition( pos ); scrollBar->setValue( pos );
} }
int wxWindowQt::GetScrollPos( int orientation ) const int wxWindowQt::GetScrollPos( int orientation ) const
{ {
wxScrollBar *scrollBar = QtGetScrollBar( orientation ); QScrollBar *scrollBar = QtGetScrollBar( orientation );
wxCHECK_MSG( scrollBar, 0, "Invalid scrollbar" ); wxCHECK_MSG( scrollBar, 0, "Invalid scrollbar" );
return scrollBar->GetThumbPosition(); return scrollBar->value();
} }
int wxWindowQt::GetScrollThumb( int orientation ) const int wxWindowQt::GetScrollThumb( int orientation ) const
{ {
wxScrollBar *scrollBar = QtGetScrollBar( orientation ); QScrollBar *scrollBar = QtGetScrollBar( orientation );
wxCHECK_MSG( scrollBar, 0, "Invalid scrollbar" ); wxCHECK_MSG( scrollBar, 0, "Invalid scrollbar" );
return scrollBar->GetThumbSize(); return scrollBar->pageStep();
} }
int wxWindowQt::GetScrollRange( int orientation ) const int wxWindowQt::GetScrollRange( int orientation ) const
{ {
wxScrollBar *scrollBar = QtGetScrollBar( orientation ); QScrollBar *scrollBar = QtGetScrollBar( orientation );
wxCHECK_MSG( scrollBar, 0, "Invalid scrollbar" ); wxCHECK_MSG( scrollBar, 0, "Invalid scrollbar" );
return scrollBar->GetRange(); return scrollBar->maximum();
}
// Handle event from scrollbars
void wxWindowQt::QtOnScrollBarEvent( wxScrollEvent& event )
{
wxEventType windowEventType = 0;
// Map the scroll bar event to the corresponding scroll window event:
wxEventType scrollBarEventType = event.GetEventType();
if ( scrollBarEventType == wxEVT_SCROLL_TOP )
windowEventType = wxEVT_SCROLLWIN_TOP;
else if ( scrollBarEventType == wxEVT_SCROLL_BOTTOM )
windowEventType = wxEVT_SCROLLWIN_BOTTOM;
else if ( scrollBarEventType == wxEVT_SCROLL_PAGEUP )
windowEventType = wxEVT_SCROLLWIN_PAGEUP;
else if ( scrollBarEventType == wxEVT_SCROLL_PAGEDOWN )
windowEventType = wxEVT_SCROLLWIN_PAGEDOWN;
else if ( scrollBarEventType == wxEVT_SCROLL_LINEUP )
windowEventType = wxEVT_SCROLLWIN_LINEUP;
else if ( scrollBarEventType == wxEVT_SCROLL_LINEDOWN )
windowEventType = wxEVT_SCROLLWIN_LINEDOWN;
else if ( scrollBarEventType == wxEVT_SCROLL_THUMBTRACK )
windowEventType = wxEVT_SCROLLWIN_THUMBTRACK;
else if ( scrollBarEventType == wxEVT_SCROLL_THUMBRELEASE )
windowEventType = wxEVT_SCROLLWIN_THUMBRELEASE;
if ( windowEventType != 0 )
{
wxScrollWinEvent e( windowEventType, event.GetPosition(), event.GetOrientation() );
ProcessWindowEvent( e );
}
} }
// scroll window to the specified position // scroll window to the specified position