X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8d813b20f78c6e24d7e0f3b8dbe8de4cb3689db4..9f39393d9efe16244c8d213076582dfd996cb129:/src/generic/listctrl.cpp diff --git a/src/generic/listctrl.cpp b/src/generic/listctrl.cpp index f6a25a839f..93b43f1529 100644 --- a/src/generic/listctrl.cpp +++ b/src/generic/listctrl.cpp @@ -8,16 +8,6 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -/* - FIXME for virtual list controls - - +1. clicking on the item with a mouse is awfully slow, what is going on? - note that selecting with keyboard seems to be much faster - => fixed HighlightAll() - iterating over 1000000 items *is* slow - - 2. background colour is wrong? - */ - /* TODO for better virtual list control support: @@ -41,6 +31,10 @@ ... we have it ourselves ... else line->GetFoo(); + + => done + + 5. attributes support: we need OnGetItemAttr() as well! */ // ============================================================================ @@ -56,10 +50,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" @@ -100,6 +90,7 @@ DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK) DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK) DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK) DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED) +DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT) // ---------------------------------------------------------------------------- // constants @@ -174,7 +165,14 @@ public: bool SelectItem(size_t item, bool select = TRUE); // select the range of items - void SelectRange(size_t itemFrom, size_t itemTo, bool select = TRUE); + // + // return true and fill the itemsChanged array with the indices of items + // which have changed state if "few" of them did, otherwise return false + // (meaning that too many items changed state to bother counting them + // individually) + bool SelectRange(size_t itemFrom, size_t itemTo, + bool select = TRUE, + wxArrayInt *itemsChanged = NULL); // return true if the given item is selected bool IsSelected(size_t item) const; @@ -212,7 +210,7 @@ class WXDLLEXPORT wxListItemData { public: wxListItemData(wxListMainWindow *owner); - ~wxListItemData() { delete m_attr; delete m_rect; } + ~wxListItemData(); void SetItem( const wxListItem &info ); void SetImage( int image ) { m_image = image; } @@ -247,7 +245,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 +383,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 ); @@ -412,10 +414,10 @@ private: // get the mode (i.e. style) of the list control inline int GetMode() const; - void SetAttributes(wxDC *dc, + // prepare the DC for drawing with these item's attributes, return true if + // we need to draw the items background to highlight it, false otherwise + bool SetAttributes(wxDC *dc, const wxListItemAttr *attr, - const wxColour& colText, - const wxFont& font, bool highlight); // these are only used by GetImage/SetImage above, we don't support images @@ -575,12 +577,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 @@ -606,12 +613,17 @@ public: // return the hit code for the corresponding position (in this line) long HitTestLine(size_t line, int x, int y) const; + // bring the selected item into view, scrolling to it if necessary + void MoveToItem(size_t item); + + // bring the current item into view + void MoveToFocus() { MoveToItem(m_current); } + void EditLabel( long item ); void OnRenameTimer(); void OnRenameAccept(); void OnMouse( wxMouseEvent &event ); - void MoveToFocus(); // called to switch the selection from the current item to newCurrent, void OnArrowChar( size_t newCurrent, const wxKeyEvent& event ); @@ -652,7 +664,7 @@ public: int GetSelectedItemCount(); // set the scrollbars and update the positions of the items - void RecalculatePositions(); + void RecalculatePositions(bool noRefresh = FALSE); // refresh the window and the header void RefreshAll(); @@ -809,6 +821,9 @@ private: // initialize the current item if needed void UpdateCurrent(); + // delete all items but don't refresh: called from dtor + void DoDeleteAllItems(); + // called when an item is [un]focuded, i.e. becomes [not] current // // currently unused @@ -877,12 +892,19 @@ bool wxSelectionStore::SelectItem(size_t item, bool select) return FALSE; } -void wxSelectionStore::SelectRange(size_t itemFrom, size_t itemTo, bool select) +bool wxSelectionStore::SelectRange(size_t itemFrom, size_t itemTo, + bool select, + wxArrayInt *itemsChanged) { + // 100 is hardcoded but it shouldn't matter much: the important thing is + // that we don't refresh everything when really few (e.g. 1 or 2) items + // change state + static const size_t MANY_ITEMS = 100; + wxASSERT_MSG( itemFrom <= itemTo, _T("should be in order") ); // are we going to have more [un]selected items than the other ones? - if ( itemTo - itemFrom > m_count / 2 ) + if ( itemTo - itemFrom > m_count/2 ) { if ( select != m_defaultState ) { @@ -909,6 +931,9 @@ void wxSelectionStore::SelectRange(size_t itemFrom, size_t itemTo, bool select) if ( selOld.Index(item) == wxNOT_FOUND ) m_itemsSel.Add(item); } + + // many items (> half) changed state + itemsChanged = NULL; } else // select == m_defaultState { @@ -932,6 +957,17 @@ void wxSelectionStore::SelectRange(size_t itemFrom, size_t itemTo, bool select) // delete all of them (from end to avoid changing indices) for ( int i = end; i >= (int)start; i-- ) { + if ( itemsChanged ) + { + if ( itemsChanged->GetCount() > MANY_ITEMS ) + { + // stop counting (see comment below) + itemsChanged = NULL; + } + + itemsChanged->Add(m_itemsSel[i]); + } + m_itemsSel.RemoveAt(i); } } @@ -939,12 +975,31 @@ void wxSelectionStore::SelectRange(size_t itemFrom, size_t itemTo, bool select) } else // "few" items change state { + if ( itemsChanged ) + { + itemsChanged->Empty(); + } + // just add the items to the selection for ( size_t item = itemFrom; item <= itemTo; item++ ) { - SelectItem(item, select); + if ( SelectItem(item, select) && itemsChanged ) + { + itemsChanged->Add(item); + + if ( itemsChanged->GetCount() > MANY_ITEMS ) + { + // stop counting them, we'll just eat gobs of memory + // for nothing at all - faster to refresh everything in + // this case + itemsChanged = NULL; + } + } } } + + // we set it to NULL if there are many items changing state + return itemsChanged != NULL; } void wxSelectionStore::OnItemDelete(size_t item) @@ -956,6 +1011,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 @@ -972,6 +1029,18 @@ void wxSelectionStore::OnItemDelete(size_t item) // wxListItemData //----------------------------------------------------------------------------- +wxListItemData::~wxListItemData() +{ + // in the virtual list control the attributes are managed by the main + // program, so don't delete them + if ( !m_owner->IsVirtual() ) + { + delete m_attr; + } + + delete m_rect; +} + void wxListItemData::Init() { m_image = -1; @@ -986,7 +1055,7 @@ wxListItemData::wxListItemData(wxListMainWindow *owner) m_owner = owner; - if ( owner->HasFlag(wxLC_REPORT) ) + if ( owner->InReportView() ) { m_rect = NULL; } @@ -1468,32 +1537,86 @@ int wxListLineData::GetImage( int index ) const return item->GetImage(); } -void wxListLineData::SetAttributes(wxDC *dc, +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); +} + +bool wxListLineData::SetAttributes(wxDC *dc, const wxListItemAttr *attr, - const wxColour& colText, - const wxFont& font, - bool highlight) + bool highlighted) { - // don't use foregroud colour for drawing highlighted items - this might + wxWindow *listctrl = m_owner->GetParent(); + + // fg colour + + // don't use foreground colour for drawing highlighted items - this might // make them completely invisible (and there is no way to do bit // arithmetics on wxColour, unfortunately) - if ( !highlight && attr && attr->HasTextColour() ) + wxColour colText; + if ( highlighted ) { - dc->SetTextForeground(attr->GetTextColour()); + colText = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT); } else { - dc->SetTextForeground(colText); + if ( attr && attr->HasTextColour() ) + { + colText = attr->GetTextColour(); + } + else + { + colText = listctrl->GetForegroundColour(); + } } + dc->SetTextForeground(colText); + + // font + wxFont font; if ( attr && attr->HasFont() ) { - dc->SetFont(attr->GetFont()); + font = attr->GetFont(); } else { - dc->SetFont(font); + font = listctrl->GetFont(); + } + + dc->SetFont(font); + + // bg colour + bool hasBgCol = attr && attr->HasBackgroundColour(); + if ( highlighted || hasBgCol ) + { + if ( highlighted ) + { + dc->SetBrush( *m_owner->m_highlightBrush ); + } + else + { + dc->SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID)); + } + + dc->SetPen( *wxTRANSPARENT_PEN ); + + return TRUE; } + + return FALSE; } void wxListLineData::Draw( wxDC *dc ) @@ -1501,6 +1624,15 @@ void wxListLineData::Draw( wxDC *dc ) wxListItemDataList::Node *node = m_items.GetFirst(); wxCHECK_RET( node, _T("no subitems at all??") ); + bool highlighted = IsHighlighted(); + + wxListItemAttr *attr = GetAttr(); + + if ( SetAttributes(dc, attr, highlighted) ) + { + dc->DrawRectangle( m_gi->m_rectHighlight ); + } + wxListItemData *item = node->GetData(); if (item->HasImage()) { @@ -1517,60 +1649,20 @@ void wxListLineData::Draw( wxDC *dc ) } void wxListLineData::DrawInReportMode( wxDC *dc, - const wxRect& r, + const wxRect& rect, const wxRect& rectHL, bool highlighted ) { - wxRect rect = r; -#if 0 - m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); - - if ( !m_owner->IsExposed( rect ) ) - return; -#endif - // use our own flag if we maintain it if ( !IsVirtual() ) highlighted = m_highlighted; - // default foreground colour - wxWindow *listctrl = m_owner->GetParent(); - wxColour colText; - if ( highlighted ) - { - colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ); - } - else - { - colText = listctrl->GetForegroundColour(); - } - - // 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(); - SetAttributes(dc, attr, colText, font, highlighted); - - bool hasBgCol = attr && attr->HasBackgroundColour(); - if ( highlighted || hasBgCol ) + // TODO: later we should support setting different attributes for + // different columns - to do it, just add "col" argument to + // GetAttr() and move these lines into the loop below + wxListItemAttr *attr = GetAttr(); + if ( SetAttributes(dc, attr, highlighted) ) { - if ( highlighted ) - { - dc->SetBrush( *m_owner->m_highlightBrush ); - } - else - { - if ( hasBgCol ) - dc->SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID)); - else - dc->SetBrush( * wxWHITE_BRUSH ); - } - - dc->SetPen( * wxTRANSPARENT_PEN ); dc->DrawRectangle( rectHL ); } @@ -1578,9 +1670,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 ) { @@ -1591,18 +1682,18 @@ 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++); - wxDCClipper clipper(*dc, 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 ); } x = xOld + width; @@ -2127,7 +2218,7 @@ wxListMainWindow::wxListMainWindow( wxWindow *parent, wxListMainWindow::~wxListMainWindow() { - DeleteEverything(); + DoDeleteAllItems(); delete m_highlightBrush; @@ -2147,6 +2238,7 @@ void wxListMainWindow::CacheLineData(size_t line) } ld->SetImage(listctrl->OnGetItemImage(line)); + ld->SetAttr(listctrl->OnGetItemAttr(line)); } wxListLineData *wxListMainWindow::GetDummyLine() const @@ -2291,25 +2383,37 @@ bool wxListMainWindow::IsHighlighted(size_t line) const } } -void wxListMainWindow::HighlightLines( size_t lineFrom, size_t lineTo, bool highlight ) +void wxListMainWindow::HighlightLines( size_t lineFrom, + size_t lineTo, + bool highlight ) { if ( IsVirtual() ) { - m_selStore.SelectRange(lineFrom, lineTo, highlight); - RefreshLines(lineFrom, lineTo); + wxArrayInt linesChanged; + if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight, + &linesChanged) ) + { + // meny items changed state, refresh everything + RefreshLines(lineFrom, lineTo); + } + else // only a few items changed state, refresh only them + { + size_t count = linesChanged.GetCount(); + for ( size_t n = 0; n < count; n++ ) + { + RefreshLine(linesChanged[n]); + } + } } - else + else // iterate over all items in non report view { - // do it the dumb way - bool needsRefresh = FALSE; for ( size_t line = lineFrom; line <= lineTo; line++ ) { if ( HighlightLine(line, highlight) ) - needsRefresh = TRUE; + { + RefreshLine(line); + } } - - if ( needsRefresh ) - RefreshLines(lineFrom, lineTo); } } @@ -2351,6 +2455,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; @@ -2365,7 +2471,7 @@ void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo ) rect.x = 0; rect.y = GetLineY(lineFrom); rect.width = GetClientSize().x; - rect.height = GetLineY(lineTo) - rect.y; + rect.height = GetLineY(lineTo) - rect.y + GetLineHeight(); CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); RefreshRect( rect ); @@ -2380,6 +2486,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 +2527,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 +2548,35 @@ void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) size_t visibleFrom, visibleTo; GetVisibleLinesRange(&visibleFrom, &visibleTo); + + wxRect rectLine; + wxCoord xOrig, yOrig; + CalcUnscrolledPosition(0, 0, &xOrig, &yOrig); + + // tell the caller cache to cache the data + if ( IsVirtual() ) + { + wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT, + GetParent()->GetId()); + evCache.SetEventObject( GetParent() ); + evCache.m_oldItemIndex = visibleFrom; + evCache.m_itemIndex = visibleTo; + GetParent()->GetEventHandler()->ProcessEvent( evCache ); + } + 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) ); } @@ -2514,7 +2680,12 @@ void wxListMainWindow::SendNotify( size_t line, if ( point != wxDefaultPosition ) le.m_pointDrag = point; - GetLine(line)->GetItem( 0, le.m_item ); + if ( command != wxEVT_COMMAND_LIST_DELETE_ITEM ) + { + GetLine(line)->GetItem( 0, le.m_item ); + } + //else: there may be no more such item + GetParent()->GetEventHandler()->ProcessEvent( le ); } @@ -2623,7 +2794,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 +2803,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; } } @@ -2770,12 +2944,12 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event ) } } -void wxListMainWindow::MoveToFocus() +void wxListMainWindow::MoveToItem(size_t item) { - if ( !HasCurrent() ) + if ( item == (size_t)-1 ) return; - wxRect rect = GetLineRect(m_current); + wxRect rect = GetLineRect(item); int client_w, client_h; GetClientSize( &client_w, &client_h ); @@ -3313,19 +3487,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 ); } } @@ -3451,7 +3627,10 @@ void wxListMainWindow::SetItemCount(long count) m_selStore.SetItemCount(count); m_countVirt = count; - Refresh(); + ResetVisibleLinesRange(); + + // scrollbars must be reset + m_dirty = TRUE; } int wxListMainWindow::GetSelectedItemCount() @@ -3508,11 +3687,8 @@ bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) // geometry calculation // ---------------------------------------------------------------------------- -void wxListMainWindow::RecalculatePositions() +void wxListMainWindow::RecalculatePositions(bool noRefresh) { - if ( IsEmpty() ) - return; - wxClientDC dc( this ); dc.SetFont( GetFont() ); @@ -3549,18 +3725,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 { @@ -3625,10 +3789,13 @@ void wxListMainWindow::RecalculatePositions() SetScrollbars( m_xScroll, m_yScroll, (entireWidth+SCROLL_UNIT_X) / m_xScroll, 0, scroll_pos, 0, TRUE ); } - // FIXME: why should we call it from here? - UpdateCurrent(); + if ( !noRefresh ) + { + // FIXME: why should we call it from here? + UpdateCurrent(); - RefreshAll(); + RefreshAll(); + } } void wxListMainWindow::RefreshAll() @@ -3709,26 +3876,26 @@ void wxListMainWindow::DeleteItem( long lindex ) size_t index = (size_t)lindex; - m_dirty = TRUE; - - // select the next item when the selected one is deleted - if ( m_current == index ) + // we don't need to adjust the index for the previous items + if ( HasCurrent() && m_current >= index ) { - // the last valid index after deleting the item will be count-2 - if ( m_current == count - 1 ) + // if the current item is being deleted, we want the next one to + // become selected - unless there is no next one - so don't adjust + // m_current in this case + if ( m_current != index || m_current == count - 1 ) { m_current--; } } - 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 +3904,11 @@ void wxListMainWindow::DeleteItem( long lindex ) m_lines.RemoveAt( index ); } - RefreshLines(index, GetItemCount() - 1); + m_dirty = TRUE; + + SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM ); + + RefreshAfter(index); } void wxListMainWindow::DeleteColumn( int col ) @@ -3750,7 +3921,7 @@ void wxListMainWindow::DeleteColumn( int col ) m_columns.DeleteNode( node ); } -void wxListMainWindow::DeleteAllItems() +void wxListMainWindow::DoDeleteAllItems() { if ( IsEmpty() ) { @@ -3758,8 +3929,6 @@ void wxListMainWindow::DeleteAllItems() return; } - m_dirty = TRUE; - ResetCurrent(); // to make the deletion of all items faster, we don't send the @@ -3775,12 +3944,22 @@ void wxListMainWindow::DeleteAllItems() { m_countVirt = 0; + m_selStore.Clear(); + } + + if ( InReportView() ) + { ResetVisibleLinesRange(); } m_lines.Clear(); +} - m_selStore.Clear(); +void wxListMainWindow::DeleteAllItems() +{ + DoDeleteAllItems(); + + RecalculatePositions(); } void wxListMainWindow::DeleteEverything() @@ -3800,14 +3979,15 @@ void wxListMainWindow::EnsureVisible( long index ) _T("invalid index in EnsureVisible") ); // We have to call this here because the label in question might just have - // been added and no screen update taken place. - if (m_dirty) - wxSafeYield(); + // been added and its position is not known yet + if ( m_dirty ) + { + m_dirty = FALSE; - size_t oldCurrent = m_current; - m_current = (size_t)index; - MoveToFocus(); - m_current = oldCurrent; + RecalculatePositions(TRUE /* no refresh */); + } + + MoveToItem((size_t)index); } long wxListMainWindow::FindItem(long start, const wxString& str, bool WXUNUSED(partial) ) @@ -3910,6 +4090,7 @@ void wxListMainWindow::InsertItem( wxListItem &item ) m_lines.Insert( line, id ); + m_dirty = TRUE; RefreshLines(id, GetItemCount() - 1); } @@ -4006,17 +4187,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 ) @@ -4121,6 +4317,7 @@ void wxListEvent::CopyObject(wxObject& object_dest) const // ------------------------------------------------------------------------------------- IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl) +IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl) BEGIN_EVENT_TABLE(wxListCtrl,wxControl) EVT_SIZE(wxListCtrl::OnSize) @@ -4245,36 +4442,35 @@ void wxListCtrl::SetWindowStyleFlag( long flag ) { m_mainWin->DeleteEverything(); - int width = 0; - int height = 0; - GetClientSize( &width, &height ); + // has the header visibility changed? + bool hasHeader = HasFlag(wxLC_REPORT) && !HasFlag(wxLC_NO_HEADER), + willHaveHeader = (flag & wxLC_REPORT) && !(flag & wxLC_NO_HEADER); - if (flag & wxLC_REPORT) + if ( hasHeader != willHaveHeader ) { - if (!HasFlag(wxLC_REPORT)) + // toggle it + if ( hasHeader ) + { + if ( m_headerWin ) + { + // don't delete, just hide, as we can reuse it later + m_headerWin->Show(FALSE); + } + //else: nothing to do + } + else // must show header { if (!m_headerWin) { CreateHeaderWindow(); - - if (HasFlag(wxLC_NO_HEADER)) - m_headerWin->Show( FALSE ); } - else + else // already have it, just show { - if (flag & wxLC_NO_HEADER) - m_headerWin->Show( FALSE ); - else - m_headerWin->Show( TRUE ); + m_headerWin->Show( TRUE ); } } - } - else // !report - { - if ( m_mainWin->HasHeader() ) - { - m_headerWin->Show( FALSE ); - } + + ResizeReportView(willHaveHeader); } } @@ -4665,10 +4861,17 @@ void wxListCtrl::OnSize(wxSizeEvent& event) if ( !m_mainWin ) return; + ResizeReportView(m_mainWin->HasHeader()); + + m_mainWin->RecalculatePositions(); +} + +void wxListCtrl::ResizeReportView(bool showHeader) +{ int cw, ch; GetClientSize( &cw, &ch ); - if ( m_mainWin->HasHeader() ) + if ( showHeader ) { m_headerWin->SetSize( 0, 0, cw, HEADER_HEIGHT ); m_mainWin->SetSize( 0, HEADER_HEIGHT + 1, cw, ch - HEADER_HEIGHT - 1 ); @@ -4677,8 +4880,6 @@ void wxListCtrl::OnSize(wxSizeEvent& event) { m_mainWin->SetSize( 0, 0, cw, ch ); } - - m_mainWin->RecalculatePositions(); } void wxListCtrl::OnIdle( wxIdleEvent & event ) @@ -4816,6 +5017,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 +5033,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