Add wxListCtrl::IsVisible()

Allow checking if the given item is (at least partially) visible on
screen.

Closes https://github.com/wxWidgets/wxWidgets/pull/1444

Closes #9949.
This commit is contained in:
oneeyeman1 2019-08-04 20:09:03 -05:00 committed by Vadim Zeitlin
parent 3bb74df215
commit 43c519e04f
8 changed files with 73 additions and 2 deletions

View File

@ -130,6 +130,7 @@ public:
bool EndEditLabel(bool cancel);
wxTextCtrl* GetEditControl() const;
bool IsVisible(long item) const wxOVERRIDE;
void Edit( long item ) { EditLabel(item); }
bool EnsureVisible( long item );

View File

@ -400,6 +400,9 @@ public:
bool InReportView() const { return HasFlag(wxLC_REPORT); }
bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL); }
// Check if the item is visible
virtual bool IsVisible(long WXUNUSED(item)) const { return false; }
// Enable or disable beep when incremental match doesn't find any item.
// Only implemented in the generic version currently.
virtual void EnableBellOnNoMatch(bool WXUNUSED(on) = true) { }

View File

@ -154,6 +154,9 @@ public:
// Gets information about the item
bool GetItem(wxListItem& info) const;
// Check if the item is visible
bool IsVisible(long item) const wxOVERRIDE;
// Sets information about the item
bool SetItem(wxListItem& info);

View File

@ -1088,6 +1088,16 @@ public:
*/
void SetImageList(wxImageList* imageList, int which);
/**
Check if the item is visible.
An item is considered visible if at least one pixel of it is present
on the screen.
@since 3.1.3
*/
bool IsVisible(long item) const;
/**
Sets the data of an item.

View File

@ -151,6 +151,7 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
EVT_MENU(LIST_TOGGLE_HEADER, MyFrame::OnToggleHeader)
EVT_MENU(LIST_TOGGLE_BELL, MyFrame::OnToggleBell)
EVT_MENU(LIST_CHECKVISIBILITY, MyFrame::OnCheckVisibility)
#ifdef __WXOSX__
EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
#endif // __WXOSX__
@ -267,6 +268,7 @@ MyFrame::MyFrame(const wxString& title)
menuList->AppendCheckItem(LIST_TOGGLE_HEADER, "Toggle &header\tCtrl-H");
menuList->Check(LIST_TOGGLE_HEADER, true);
menuList->AppendCheckItem(LIST_TOGGLE_BELL, "Toggle &bell on no match");
menuList->Append( LIST_CHECKVISIBILITY, "Check if lines 2 and 9 are visible" );
menuList->AppendSeparator();
menuList->AppendCheckItem(LIST_TOGGLE_CHECKBOXES,
"&Enable Checkboxes");
@ -377,6 +379,18 @@ void MyFrame::OnToggleBell(wxCommandEvent& event)
m_listCtrl->EnableBellOnNoMatch(event.IsChecked());
}
void MyFrame::OnCheckVisibility(wxCommandEvent& WXUNUSED(event))
{
if ( m_listCtrl->IsVisible(2) )
wxLogMessage( "Line 2 is visible" );
else
wxLogMessage( "Line 2 is not visible" );
if ( m_listCtrl->IsVisible(9) )
wxLogMessage( "Line 9 is visible" );
else
wxLogMessage( "Line 9 is not visible" );
}
#ifdef __WXOSX__
void MyFrame::OnToggleMacUseGeneric(wxCommandEvent& event)

View File

@ -117,7 +117,7 @@ protected:
void OnSmallIconTextView(wxCommandEvent& event);
void OnVirtualView(wxCommandEvent& event);
void OnSmallVirtualView(wxCommandEvent& event);
void OnCheckVisibility(wxCommandEvent& event);
void OnSetItemsCount(wxCommandEvent& event);
@ -247,6 +247,6 @@ enum
#ifdef __WXOSX__
LIST_MAC_USE_GENERIC,
#endif
LIST_CHECKVISIBILITY,
LIST_CTRL = 1000
};

View File

@ -151,6 +151,23 @@ wxListItemData::wxListItemData(wxListMainWindow *owner)
m_rect = new wxRect;
}
// Check if the item is visible
bool wxGenericListCtrl::IsVisible(long item) const
{
wxRect itemRect;
GetItemRect( item, itemRect );
const wxRect clientRect = GetClientRect();
bool visible = clientRect.Intersects( itemRect );
if ( visible && m_headerWin )
{
wxRect headerRect = m_headerWin->GetClientRect();
// take into account the +1 added in GetSubItemRect()
headerRect.height++;
visible = itemRect.GetBottom() > headerRect.GetBottom();
}
return visible;
}
void wxListItemData::SetItem( const wxListItem &info )
{
if ( info.m_mask & wxLIST_MASK_TEXT )

View File

@ -67,6 +67,10 @@
#define NO_ITEM (-1)
#endif
#ifndef LVM_ISITEMVISIBLE
#define LVM_ISITEMVISIBLE (LVM_FIRST + 182)
#endif
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------
@ -880,6 +884,25 @@ bool wxListCtrl::GetItem(wxListItem& info) const
return success;
}
// Check if the item is visible
bool wxListCtrl::IsVisible(long item) const
{
bool result = ::SendMessage( GetHwnd(), LVM_ISITEMVISIBLE, (WPARAM) item, 0 ) != 0;
if ( result )
{
HWND hwndHdr = ListView_GetHeader(GetHwnd());
wxRect itemRect;
RECT headerRect;
if ( Header_GetItemRect( hwndHdr, 0, &headerRect ) )
{
GetItemRect( item, itemRect );
wxRect rectHeader = wxRectFromRECT( headerRect );
result = itemRect.GetBottom() > rectHeader.GetBottom();
}
}
return result;
}
// Sets information about the item
bool wxListCtrl::SetItem(wxListItem& info)
{