Improve wxListBox::GetCountPerPage() in wxGTK and wxOSX

Provide native implementation of this function instead of using the ad hoc one
in common code, which didn't really work -- so remove it completely now.

Closes #17189.
This commit is contained in:
Andreas Falkenhahn 2017-04-07 00:04:20 +02:00 committed by Vadim Zeitlin
parent accf7ab117
commit e77cb6f31f
9 changed files with 55 additions and 37 deletions

View File

@ -75,6 +75,7 @@ public:
virtual void EnsureVisible(int n) wxOVERRIDE; virtual void EnsureVisible(int n) wxOVERRIDE;
virtual int GetTopItem() const wxOVERRIDE; virtual int GetTopItem() const wxOVERRIDE;
virtual int GetCountPerPage() const wxOVERRIDE;
virtual void Update() wxOVERRIDE; virtual void Update() wxOVERRIDE;

View File

@ -73,7 +73,7 @@ public:
virtual void EnsureVisible(int n); virtual void EnsureVisible(int n);
virtual int GetTopItem() const { return wxNOT_FOUND; } virtual int GetTopItem() const { return wxNOT_FOUND; }
virtual int GetCountPerPage() const; virtual int GetCountPerPage() const { return -1; }
// a combination of Append() and EnsureVisible(): appends the item to the // a combination of Append() and EnsureVisible(): appends the item to the
// listbox and ensures that it is visible i.e. not scrolled out of view // listbox and ensures that it is visible i.e. not scrolled out of view
@ -136,8 +136,6 @@ protected:
// single selection mode on platforms other than MSW). // single selection mode on platforms other than MSW).
void UpdateOldSelections(); void UpdateOldSelections();
wxCoord GetLineHeight() const;
private: private:
wxDECLARE_NO_COPY_CLASS(wxListBoxBase); wxDECLARE_NO_COPY_CLASS(wxListBoxBase);
}; };

View File

@ -615,6 +615,7 @@ public:
virtual void ListScrollTo( unsigned int n ) = 0; virtual void ListScrollTo( unsigned int n ) = 0;
virtual int ListGetTopItem() const = 0; virtual int ListGetTopItem() const = 0;
virtual int ListGetCountPerPage() const = 0;
virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) = 0; virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) = 0;
virtual void UpdateLineToEnd( unsigned int n) = 0; virtual void UpdateLineToEnd( unsigned int n) = 0;

View File

@ -109,6 +109,7 @@ public:
virtual void EnsureVisible(int n) wxOVERRIDE; virtual void EnsureVisible(int n) wxOVERRIDE;
virtual int GetTopItem() const wxOVERRIDE; virtual int GetTopItem() const wxOVERRIDE;
virtual int GetCountPerPage() const wxOVERRIDE;
virtual wxVisualAttributes GetDefaultAttributes() const wxOVERRIDE virtual wxVisualAttributes GetDefaultAttributes() const wxOVERRIDE
{ {

View File

@ -301,7 +301,9 @@ public:
Return the number of items that can fit vertically in the visible area of Return the number of items that can fit vertically in the visible area of
the listbox. the listbox.
Returns -1 if the number of items per page couldn't be determined. Returns -1 if the number of items per page couldn't be determined. On
wxGTK this method can only determine the number of items per page if
there is at least one item in the listbox.
@since 3.1.0 @since 3.1.0
*/ */

View File

@ -34,15 +34,6 @@
#include "wx/dcclient.h" #include "wx/dcclient.h"
#endif #endif
// the spacing between the lines (in report mode)
static const int LINE_SPACING = 0;
#ifdef __WXGTK__
static const int EXTRA_HEIGHT = 6;
#else
static const int EXTRA_HEIGHT = 4;
#endif
extern WXDLLEXPORT_DATA(const char) wxListBoxNameStr[] = "listBox"; extern WXDLLEXPORT_DATA(const char) wxListBoxNameStr[] = "listBox";
// ============================================================================ // ============================================================================
@ -349,24 +340,4 @@ void wxListBoxBase::EnsureVisible(int WXUNUSED(n))
// the base class version does nothing (the only alternative would be to // the base class version does nothing (the only alternative would be to
// call SetFirstItem() but this is probably even more stupid) // call SetFirstItem() but this is probably even more stupid)
} }
wxCoord wxListBoxBase::GetLineHeight() const
{
wxListBoxBase *self = wxConstCast(this, wxListBoxBase);
wxClientDC dc( self );
dc.SetFont( GetFont() );
wxCoord y;
dc.GetTextExtent(wxT("H"), NULL, &y);
y += EXTRA_HEIGHT;
return y + LINE_SPACING;
}
int wxListBoxBase::GetCountPerPage() const
{
return GetClientSize().y / GetLineHeight();
}
#endif // wxUSE_LISTBOX #endif // wxUSE_LISTBOX

View File

@ -780,6 +780,37 @@ int wxListBox::GetTopItem() const
return idx; return idx;
} }
int wxListBox::GetCountPerPage() const
{
wxGtkTreePath path;
GtkTreeViewColumn *column;
if ( !gtk_tree_view_get_path_at_pos
(
m_treeview,
0,
0,
path.ByRef(),
&column,
NULL,
NULL
) )
{
return -1;
}
GdkRectangle rect;
gtk_tree_view_get_cell_area(m_treeview, path, column, &rect);
if ( !rect.height )
return -1;
GdkRectangle vis;
gtk_tree_view_get_visible_rect(m_treeview, &vis);
return vis.height / rect.height;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// hittest // hittest
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -130,6 +130,7 @@ public :
virtual void ListScrollTo( unsigned int n ) wxOVERRIDE ; virtual void ListScrollTo( unsigned int n ) wxOVERRIDE ;
virtual int ListGetTopItem() const wxOVERRIDE; virtual int ListGetTopItem() const wxOVERRIDE;
virtual int ListGetCountPerPage() const wxOVERRIDE;
// accessing content // accessing content
@ -548,6 +549,13 @@ int wxListWidgetCocoaImpl::ListGetTopItem() const
return range.location; return range.location;
} }
int wxListWidgetCocoaImpl::ListGetCountPerPage() const
{
NSScrollView *scrollView = [m_tableView enclosingScrollView];
NSRect visibleRect = scrollView.contentView.visibleRect;
return (int) (visibleRect.size.height / [m_tableView rowHeight]);
}
void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) ) void wxListWidgetCocoaImpl::UpdateLine( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) )
{ {
// TODO optimize // TODO optimize

View File

@ -150,6 +150,11 @@ int wxListBox::GetTopItem() const
return GetListPeer()->ListGetTopItem(); return GetListPeer()->ListGetTopItem();
} }
int wxListBox::GetCountPerPage() const
{
return GetListPeer()->ListGetCountPerPage();
}
void wxListBox::DoDeleteOneItem(unsigned int n) void wxListBox::DoDeleteOneItem(unsigned int n)
{ {
wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::Delete") ); wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::Delete") );