diff --git a/include/wx/generic/listctrl.h b/include/wx/generic/listctrl.h index 040035ba72..d377082e7b 100644 --- a/include/wx/generic/listctrl.h +++ b/include/wx/generic/listctrl.h @@ -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 ); diff --git a/include/wx/listbase.h b/include/wx/listbase.h index 4a41262e5c..8c493bc935 100644 --- a/include/wx/listbase.h +++ b/include/wx/listbase.h @@ -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) { } diff --git a/include/wx/msw/listctrl.h b/include/wx/msw/listctrl.h index 78da57b6cc..8a3908ffbe 100644 --- a/include/wx/msw/listctrl.h +++ b/include/wx/msw/listctrl.h @@ -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); diff --git a/interface/wx/listctrl.h b/interface/wx/listctrl.h index d90b30d19c..6b9edb72d5 100644 --- a/interface/wx/listctrl.h +++ b/interface/wx/listctrl.h @@ -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. diff --git a/samples/listctrl/listtest.cpp b/samples/listctrl/listtest.cpp index 6ce7a10cb0..3a7f73702e 100644 --- a/samples/listctrl/listtest.cpp +++ b/samples/listctrl/listtest.cpp @@ -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) diff --git a/samples/listctrl/listtest.h b/samples/listctrl/listtest.h index dcdca2a55c..fb1cfd5e7d 100644 --- a/samples/listctrl/listtest.h +++ b/samples/listctrl/listtest.h @@ -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 }; diff --git a/src/generic/listctrl.cpp b/src/generic/listctrl.cpp index b76844b4bf..fdf2cd43a1 100644 --- a/src/generic/listctrl.cpp +++ b/src/generic/listctrl.cpp @@ -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 ) diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp index dcdfa772dc..9adb563fe7 100644 --- a/src/msw/listctrl.cpp +++ b/src/msw/listctrl.cpp @@ -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) {