X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5cd891743efcff4d6603c68b98e72fb033792a88..53e46b61bbe4ce72b92c8898907f0a879d5f012b:/src/generic/listctrl.cpp diff --git a/src/generic/listctrl.cpp b/src/generic/listctrl.cpp index 725faf3c0f..d2f3b99675 100644 --- a/src/generic/listctrl.cpp +++ b/src/generic/listctrl.cpp @@ -41,6 +41,10 @@ ... we have it ourselves ... else line->GetFoo(); + + => done + + 5. attributes support: we need OnGetItemAttr() as well! */ // ============================================================================ @@ -56,10 +60,6 @@ #pragma implementation "listctrlbase.h" #endif -#if 0 -#include "listctrl.old.cpp" -#else - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -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 ); @@ -575,12 +579,17 @@ public: void ReverseHighlight( size_t line ) { HighlightLine(line, !IsHighlighted(line)); RefreshLine(line); } + // return true if the line is highlighted + bool IsHighlighted(size_t line) const; + // refresh one or several lines at once void RefreshLine( size_t line ); void RefreshLines( size_t lineFrom, size_t lineTo ); - // return true if the line is highlighted - bool IsHighlighted(size_t line) const; + // refresh all lines below the given one: the difference with + // RefreshLines() is that the index here might not be a valid one (happens + // when the last line is deleted) + void RefreshAfter( size_t lineFrom ); // the methods which are forwarded to wxListLineData itself in list/icon // modes but are here because the lines don't store their positions in the @@ -956,6 +965,8 @@ void wxSelectionStore::OnItemDelete(size_t item) { // this item itself was in m_itemsSel, remove it from there m_itemsSel.RemoveAt(i); + + count--; } // and adjust the index of all which follow it @@ -1468,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, @@ -1517,16 +1546,10 @@ void wxListLineData::Draw( wxDC *dc ) } void wxListLineData::DrawInReportMode( wxDC *dc, - const wxRect& r, + const wxRect& rect, const wxRect& rectHL, bool highlighted ) { - wxRect rect = r; - //m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); - - if ( !m_owner->IsExposed( rect ) ) - return; - // use our own flag if we maintain it if ( !IsVirtual() ) highlighted = m_highlighted; @@ -1546,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(); @@ -1576,9 +1598,8 @@ void wxListLineData::DrawInReportMode( wxDC *dc, wxCHECK_RET( node, _T("no subitems at all??") ); size_t col = 0; - int x = rect.x + HEADER_OFFSET_X; - - rect.y += (LINE_SPACING + EXTRA_HEIGHT) / 2; + wxCoord x = rect.x + HEADER_OFFSET_X, + y = rect.y + (LINE_SPACING + EXTRA_HEIGHT) / 2; while ( node ) { @@ -1589,22 +1610,20 @@ void wxListLineData::DrawInReportMode( wxDC *dc, if ( item->HasImage() ) { int ix, iy; - m_owner->DrawImage( item->GetImage(), dc, x, rect.y ); + m_owner->DrawImage( item->GetImage(), dc, x, y ); m_owner->GetImageSize( item->GetImage(), ix, iy ); x += ix + 5; // FIXME: what is "5"? } int width = m_owner->GetColumnWidth(col++); - dc->SetClippingRegion(x, rect.y, width, rect.height); + wxDCClipper clipper(*dc, x, y, width, rect.height); if ( item->HasText() ) { - dc->DrawText( item->GetText(), x, rect.y ); + dc->DrawText( item->GetText(), x, y ); } - dc->DestroyClippingRegion(); - x = xOld + width; node = node->GetNext(); @@ -2147,6 +2166,7 @@ void wxListMainWindow::CacheLineData(size_t line) } ld->SetImage(listctrl->OnGetItemImage(line)); + ld->SetAttr(listctrl->OnGetItemAttr(line)); } wxListLineData *wxListMainWindow::GetDummyLine() const @@ -2351,6 +2371,8 @@ void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo ) // we suppose that they are ordered by caller wxASSERT_MSG( lineFrom <= lineTo, _T("indices in disorder") ); + wxASSERT_MSG( lineTo < GetItemCount(), _T("invalid line range") ); + if ( HasFlag(wxLC_REPORT) ) { size_t visibleFrom, visibleTo; @@ -2380,6 +2402,35 @@ void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo ) } } +void wxListMainWindow::RefreshAfter( size_t lineFrom ) +{ + if ( HasFlag(wxLC_REPORT) ) + { + size_t visibleFrom; + GetVisibleLinesRange(&visibleFrom, NULL); + + if ( lineFrom < visibleFrom ) + lineFrom = visibleFrom; + + wxRect rect; + rect.x = 0; + rect.y = GetLineY(lineFrom); + + wxSize size = GetClientSize(); + rect.width = size.x; + // refresh till the bottom of the window + rect.height = size.y - rect.y; + + CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); + RefreshRect( rect ); + } + else // !report + { + // TODO: how to do it more efficiently? + m_dirty = TRUE; + } +} + void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) { // Note: a wxPaintDC must be constructed even if no drawing is @@ -2392,6 +2443,12 @@ void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) return; } + if ( m_dirty ) + { + // delay the repainting until we calculate all the items positions + return; + } + PrepareDC( dc ); int dev_x, dev_y; @@ -2407,10 +2464,24 @@ void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) size_t visibleFrom, visibleTo; GetVisibleLinesRange(&visibleFrom, &visibleTo); + + wxRect rectLine; + wxCoord xOrig, yOrig; + CalcUnscrolledPosition(0, 0, &xOrig, &yOrig); + for ( size_t line = visibleFrom; line <= visibleTo; line++ ) { + rectLine = GetLineRect(line); + + if ( !IsExposed(rectLine.x - xOrig, rectLine.y - yOrig, + rectLine.width, rectLine.height) ) + { + // don't redraw unaffected lines to avoid flicker + continue; + } + GetLine(line)->DrawInReportMode( &dc, - GetLineRect(line), + rectLine, GetLineHighlightRect(line), IsHighlighted(line) ); } @@ -2623,7 +2694,7 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event ) int y = event.GetY(); CalcUnscrolledPosition( x, y, &x, &y ); - /* Did we actually hit an item ? */ + // where did we hit it (if we did)? long hitResult = 0; size_t count = GetItemCount(), @@ -2632,15 +2703,18 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event ) if ( HasFlag(wxLC_REPORT) ) { current = y / GetLineHeight(); - hitResult = HitTestLine(current, x, y); + if ( current < count ) + hitResult = HitTestLine(current, x, y); } else // !report { // TODO: optimize it too! this is less simple than for report view but // enumerating all items is still not a way to do it!! - for ( current = 0; current < count && !hitResult; current++ ) + for ( current = 0; current < count; current++ ) { hitResult = HitTestLine(current, x, y); + if ( hitResult ) + break; } } @@ -3313,19 +3387,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 ); } } @@ -3549,18 +3625,6 @@ void wxListMainWindow::RecalculatePositions() GetScrollPos(wxHORIZONTAL), GetScrollPos(wxVERTICAL), TRUE ); - - // FIXME: wxGTK::wxScrolledWindow doesn't have SetTargetRect() -#if !defined(__WXGTK__) || defined(__WXUNIVERSAL__) - // we must have an integer number of lines on screen and so we fit - // the real control size to the line height - wxRect rect; - rect.x = 0; - rect.y = LINE_SPACING; - rect.width = clientWidth; - rect.height = ((clientHeight - LINE_SPACING) / lineHeight)*lineHeight; - SetTargetRect(rect); -#endif } else // !report { @@ -3723,12 +3787,14 @@ void wxListMainWindow::DeleteItem( long lindex ) SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM ); + if ( InReportView() ) + { + ResetVisibleLinesRange(); + } + if ( IsVirtual() ) { - if ( m_lineTo == --m_countVirt ) - { - m_lineTo--; - } + m_countVirt--; m_selStore.OnItemDelete(index); } @@ -3737,7 +3803,8 @@ void wxListMainWindow::DeleteItem( long lindex ) m_lines.RemoveAt( index ); } - RefreshLines(index, GetItemCount() - 1); + m_dirty = TRUE; + RefreshAfter(index); } void wxListMainWindow::DeleteColumn( int col ) @@ -3778,6 +3845,11 @@ void wxListMainWindow::DeleteAllItems() ResetVisibleLinesRange(); } + if ( InReportView() ) + { + ResetVisibleLinesRange(); + } + m_lines.Clear(); m_selStore.Clear(); @@ -3910,6 +3982,7 @@ void wxListMainWindow::InsertItem( wxListItem &item ) m_lines.Insert( line, id ); + m_dirty = TRUE; RefreshLines(id, GetItemCount() - 1); } @@ -4006,17 +4079,32 @@ void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to) if ( m_lineFrom == (size_t)-1 ) { - m_lineFrom = GetScrollPos(wxVERTICAL); - size_t count = GetItemCount(); + if ( count ) + { + m_lineFrom = GetScrollPos(wxVERTICAL); - wxASSERT_MSG( m_lineFrom < count, _T("invalid scroll position?") ); + // this may happen if SetScrollbars() hadn't been called yet + if ( m_lineFrom >= count ) + m_lineFrom = count - 1; - m_lineTo = m_lineFrom + m_linesPerPage - 1; - if ( m_lineTo >= count ) - m_lineTo = count - 1; + // we redraw one extra line but this is needed to make the redrawing + // logic work when there is a fractional number of lines on screen + m_lineTo = m_lineFrom + m_linesPerPage; + if ( m_lineTo >= count ) + m_lineTo = count - 1; + } + else // empty control + { + m_lineFrom = 0; + m_lineTo = (size_t)-1; + } } + wxASSERT_MSG( IsEmpty() || + (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()), + _T("GetVisibleLinesRange() returns incorrect result") ); + if ( from ) *from = m_lineFrom; if ( to ) @@ -4816,6 +4904,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") ); @@ -4823,6 +4920,14 @@ void wxListCtrl::SetItemCount(long count) m_mainWin->SetItemCount(count); } -#endif // wxUSE_LISTCTRL +void wxListCtrl::RefreshItem(long item) +{ + m_mainWin->RefreshLine(item); +} -#endif +void wxListCtrl::RefreshItems(long itemFrom, long itemTo) +{ + m_mainWin->RefreshLines(itemFrom, itemTo); +} + +#endif // wxUSE_LISTCTRL