From e77cb6f31fcd06a8653801bfcb05984e546ce429 Mon Sep 17 00:00:00 2001 From: Andreas Falkenhahn Date: Fri, 7 Apr 2017 00:04:20 +0200 Subject: [PATCH] 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. --- include/wx/gtk/listbox.h | 1 + include/wx/listbox.h | 4 +--- include/wx/osx/core/private.h | 1 + include/wx/osx/listbox.h | 1 + interface/wx/listbox.h | 4 +++- src/common/lboxcmn.cpp | 29 ----------------------------- src/gtk/listbox.cpp | 31 +++++++++++++++++++++++++++++++ src/osx/cocoa/listbox.mm | 16 ++++++++++++---- src/osx/listbox_osx.cpp | 5 +++++ 9 files changed, 55 insertions(+), 37 deletions(-) diff --git a/include/wx/gtk/listbox.h b/include/wx/gtk/listbox.h index a99dca5c43..de55c6a418 100644 --- a/include/wx/gtk/listbox.h +++ b/include/wx/gtk/listbox.h @@ -75,6 +75,7 @@ public: virtual void EnsureVisible(int n) wxOVERRIDE; virtual int GetTopItem() const wxOVERRIDE; + virtual int GetCountPerPage() const wxOVERRIDE; virtual void Update() wxOVERRIDE; diff --git a/include/wx/listbox.h b/include/wx/listbox.h index 8f649e75a7..69525e219f 100644 --- a/include/wx/listbox.h +++ b/include/wx/listbox.h @@ -73,7 +73,7 @@ public: virtual void EnsureVisible(int n); 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 // 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). void UpdateOldSelections(); - wxCoord GetLineHeight() const; - private: wxDECLARE_NO_COPY_CLASS(wxListBoxBase); }; diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index 0d7fe86dcf..a372aa529a 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -615,6 +615,7 @@ public: virtual void ListScrollTo( unsigned int n ) = 0; virtual int ListGetTopItem() const = 0; + virtual int ListGetCountPerPage() const = 0; virtual void UpdateLine( unsigned int n, wxListWidgetColumn* col = NULL ) = 0; virtual void UpdateLineToEnd( unsigned int n) = 0; diff --git a/include/wx/osx/listbox.h b/include/wx/osx/listbox.h index 53fd434d97..e96b186d7a 100644 --- a/include/wx/osx/listbox.h +++ b/include/wx/osx/listbox.h @@ -109,6 +109,7 @@ public: virtual void EnsureVisible(int n) wxOVERRIDE; virtual int GetTopItem() const wxOVERRIDE; + virtual int GetCountPerPage() const wxOVERRIDE; virtual wxVisualAttributes GetDefaultAttributes() const wxOVERRIDE { diff --git a/interface/wx/listbox.h b/interface/wx/listbox.h index 515719a06b..a736e943f0 100644 --- a/interface/wx/listbox.h +++ b/interface/wx/listbox.h @@ -301,7 +301,9 @@ public: Return the number of items that can fit vertically in the visible area of 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 */ diff --git a/src/common/lboxcmn.cpp b/src/common/lboxcmn.cpp index 80e169ba16..16a8ba724e 100644 --- a/src/common/lboxcmn.cpp +++ b/src/common/lboxcmn.cpp @@ -34,15 +34,6 @@ #include "wx/dcclient.h" #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"; // ============================================================================ @@ -349,24 +340,4 @@ void wxListBoxBase::EnsureVisible(int WXUNUSED(n)) // the base class version does nothing (the only alternative would be to // 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 diff --git a/src/gtk/listbox.cpp b/src/gtk/listbox.cpp index 21fab1e891..c28ca872ac 100644 --- a/src/gtk/listbox.cpp +++ b/src/gtk/listbox.cpp @@ -780,6 +780,37 @@ int wxListBox::GetTopItem() const 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 // ---------------------------------------------------------------------------- diff --git a/src/osx/cocoa/listbox.mm b/src/osx/cocoa/listbox.mm index f7f85be8b0..2078be5035 100644 --- a/src/osx/cocoa/listbox.mm +++ b/src/osx/cocoa/listbox.mm @@ -130,6 +130,7 @@ public : virtual void ListScrollTo( unsigned int n ) wxOVERRIDE ; virtual int ListGetTopItem() const wxOVERRIDE; + virtual int ListGetCountPerPage() const wxOVERRIDE; // accessing content @@ -542,10 +543,17 @@ void wxListWidgetCocoaImpl::ListScrollTo( unsigned int n ) int wxListWidgetCocoaImpl::ListGetTopItem() const { - NSScrollView *scrollView = [m_tableView enclosingScrollView]; - NSRect visibleRect = scrollView.contentView.visibleRect; - NSRange range = [m_tableView rowsInRect:visibleRect]; - return range.location; + NSScrollView *scrollView = [m_tableView enclosingScrollView]; + NSRect visibleRect = scrollView.contentView.visibleRect; + NSRange range = [m_tableView rowsInRect:visibleRect]; + 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) ) diff --git a/src/osx/listbox_osx.cpp b/src/osx/listbox_osx.cpp index 85a437862c..f824848ec0 100644 --- a/src/osx/listbox_osx.cpp +++ b/src/osx/listbox_osx.cpp @@ -150,6 +150,11 @@ int wxListBox::GetTopItem() const return GetListPeer()->ListGetTopItem(); } +int wxListBox::GetCountPerPage() const +{ + return GetListPeer()->ListGetCountPerPage(); +} + void wxListBox::DoDeleteOneItem(unsigned int n) { wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::Delete") );