From 6c02c32922ecd6b3272c89669e3fe1304cd9dfac Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 11 Jul 2001 16:22:12 +0000 Subject: [PATCH] added support for item attributes in virtual list control git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@10972 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/listctrl.h | 6 ++-- include/wx/msw/listctrl.h | 3 ++ samples/listctrl/listtest.cpp | 21 ++++++++++++ samples/listctrl/listtest.h | 13 ++++++-- src/generic/listctrl.cpp | 61 +++++++++++++++++++++++++++-------- src/msw/listctrl.cpp | 9 ++++++ 6 files changed, 95 insertions(+), 18 deletions(-) diff --git a/include/wx/generic/listctrl.h b/include/wx/generic/listctrl.h index c6852204a8..7a51c47577 100644 --- a/include/wx/generic/listctrl.h +++ b/include/wx/generic/listctrl.h @@ -185,14 +185,16 @@ protected: // return the icon for the given item virtual int OnGetItemImage(long item) const; + // return the attribute for the item (may return NULL if none) + virtual wxListItemAttr *OnGetItemAttr(long item) const; + // it calls our OnGetXXX() functions friend class WXDLLEXPORT wxListMainWindow; private: // Virtual function hiding supression - void Update(void) - { wxWindowBase::Update(); } + virtual void Update() { wxWindowBase::Update(); } // create the header window void CreateHeaderWindow(); diff --git a/include/wx/msw/listctrl.h b/include/wx/msw/listctrl.h index e11e89c670..6f6a05f3c3 100644 --- a/include/wx/msw/listctrl.h +++ b/include/wx/msw/listctrl.h @@ -373,6 +373,9 @@ protected: // return the icon for the given item virtual int OnGetItemImage(long item) const; + // return the attribute for the item (may return NULL if none) + virtual wxListItemAttr *OnGetItemAttr(long item) const; + private: bool DoCreateControl(int x, int y, int w, int h); diff --git a/samples/listctrl/listtest.cpp b/samples/listctrl/listtest.cpp index b0903b6e73..d216a4cecb 100644 --- a/samples/listctrl/listtest.cpp +++ b/samples/listctrl/listtest.cpp @@ -657,6 +657,22 @@ void MyListCtrl::OnListKeyDown(wxListEvent& event) { switch ( event.GetCode() ) { + case 'c': + { + wxListItem info; + info.m_itemId = event.GetIndex(); + GetItem(info); + + wxListItemAttr *attr = info.GetAttributes(); + if ( !attr || !attr->HasTextColour() ) + { + info.SetTextColour(*wxCYAN); + + SetItem(info); + } + } + break; + case WXK_DELETE: DeleteItem(event.GetIndex()); @@ -708,6 +724,11 @@ int MyListCtrl::OnGetItemImage(long item) const return 0; } +wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const +{ + return item % 2 ? NULL : (wxListItemAttr *)&m_attr; +} + void MyListCtrl::InsertItemInReportView(int i) { wxString buf; diff --git a/samples/listctrl/listtest.h b/samples/listctrl/listtest.h index 446585a4a4..ef100dff52 100644 --- a/samples/listctrl/listtest.h +++ b/samples/listctrl/listtest.h @@ -19,9 +19,13 @@ public: class MyListCtrl: public wxListCtrl { public: - MyListCtrl(wxWindow *parent, const wxWindowID id, const wxPoint& pos, - const wxSize& size, long style): - wxListCtrl(parent, id, pos, size, style) + MyListCtrl(wxWindow *parent, + const wxWindowID id, + const wxPoint& pos, + const wxSize& size, + long style) + : wxListCtrl(parent, id, pos, size, style), + m_attr(*wxCYAN, *wxLIGHT_GREY, wxNullFont) { } @@ -49,6 +53,9 @@ private: virtual wxString OnGetItemText(long item, long column) const; virtual int OnGetItemImage(long item) const; + virtual wxListItemAttr *OnGetItemAttr(long item) const; + + wxListItemAttr m_attr; DECLARE_EVENT_TABLE() }; diff --git a/src/generic/listctrl.cpp b/src/generic/listctrl.cpp index 256271c2fd..221fc0b6f5 100644 --- a/src/generic/listctrl.cpp +++ b/src/generic/listctrl.cpp @@ -247,7 +247,8 @@ public: void GetItem( wxListItem &info ) const; - wxListItemAttr *GetAttributes() const { return m_attr; } + void SetAttr(wxListItemAttr *attr) { m_attr = attr; } + wxListItemAttr *GetAttr() const { return m_attr; } public: // the item image or -1 @@ -384,6 +385,9 @@ public: wxString GetText(int index) const; void SetText( int index, const wxString s ); + wxListItemAttr *GetAttr() const; + void SetAttr(wxListItemAttr *attr); + // return true if the highlighting really changed bool Highlight( bool on ); @@ -1475,6 +1479,24 @@ int wxListLineData::GetImage( int index ) const return item->GetImage(); } +wxListItemAttr *wxListLineData::GetAttr() const +{ + wxListItemDataList::Node *node = m_items.GetFirst(); + wxCHECK_MSG( node, NULL, _T("invalid column index in GetAttr()") ); + + wxListItemData *item = node->GetData(); + return item->GetAttr(); +} + +void wxListLineData::SetAttr(wxListItemAttr *attr) +{ + wxListItemDataList::Node *node = m_items.GetFirst(); + wxCHECK_RET( node, _T("invalid column index in SetAttr()") ); + + wxListItemData *item = node->GetData(); + item->SetAttr(attr); +} + void wxListLineData::SetAttributes(wxDC *dc, const wxListItemAttr *attr, const wxColour& colText, @@ -1547,11 +1569,10 @@ void wxListLineData::DrawInReportMode( wxDC *dc, // default font wxFont font = listctrl->GetFont(); - // VZ: currently we set the colours/fonts only once, but like this (i.e. - // using SetAttributes() inside the loop), it will be trivial to - // customize the subitems (in report mode) too. - wxListItemData *item = m_items.GetFirst()->GetData(); - wxListItemAttr *attr = item->GetAttributes(); + // TODO: later we should support setting different attributes for + // different columns - to do it, just add "col" argument to + // GetAttr() and move this code into the loop below + wxListItemAttr *attr = GetAttr(); SetAttributes(dc, attr, colText, font, highlighted); bool hasBgCol = attr && attr->HasBackgroundColour(); @@ -2145,6 +2166,7 @@ void wxListMainWindow::CacheLineData(size_t line) } ld->SetImage(listctrl->OnGetItemImage(line)); + ld->SetAttr(listctrl->OnGetItemAttr(line)); } wxListLineData *wxListMainWindow::GetDummyLine() const @@ -2458,6 +2480,8 @@ void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) continue; } + printf("Redrawing line %u\n", line); + GetLine(line)->DrawInReportMode( &dc, rectLine, GetLineHighlightRect(line), @@ -3365,19 +3389,21 @@ void wxListMainWindow::SetItem( wxListItem &item ) wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(), _T("invalid item index in SetItem") ); - if ( IsVirtual() ) + if ( !IsVirtual() ) + { + wxListLineData *line = GetLine((size_t)id); + line->SetItem( item.m_col, item ); + } + + if ( InReportView() ) { // just refresh the line to show the new value of the text/image RefreshLine((size_t)id); } - else // !virtual + else // !report { + // refresh everything (resulting in horrible flicker - FIXME!) m_dirty = TRUE; - - wxListLineData *line = GetLine((size_t)id); - if ( HasFlag(wxLC_REPORT) ) - item.m_width = GetColumnWidth( item.m_col ); - line->SetItem( item.m_col, item ); } } @@ -4880,6 +4906,15 @@ int wxListCtrl::OnGetItemImage(long item) const return -1; } +wxListItemAttr *wxListCtrl::OnGetItemAttr(long item) const +{ + wxASSERT_MSG( item >= 0 && item < GetItemCount(), + _T("invalid item index in OnGetItemAttr()") ); + + // no attributes by default + return NULL; +} + void wxListCtrl::SetItemCount(long count) { wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") ); diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp index d5bd89601f..7caeb01f58 100644 --- a/src/msw/listctrl.cpp +++ b/src/msw/listctrl.cpp @@ -1825,6 +1825,15 @@ int wxListCtrl::OnGetItemImage(long item) const return -1; } +wxListItemAttr *wxListCtrl::OnGetItemAttr(long item) const +{ + wxASSERT_MSG( item >= 0 && (size_t)item < GetItemCount(), + _T("invalid item index in OnGetItemAttr()") ); + + // no attributes by default + return NULL; +} + void wxListCtrl::SetItemCount(long count) { wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") ); -- 2.45.2