... we have it ourselves ...
else
line->GetFoo();
+
+ => done
+
+ 5. attributes support: we need OnGetItemAttr() as well!
*/
// ============================================================================
#ifdef __GNUG__
#pragma implementation "listctrl.h"
#pragma implementation "listctrlbase.h"
-#endif
+#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
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
{
public:
wxListItemData(wxListMainWindow *owner);
- ~wxListItemData() { delete m_attr; delete m_rect; }
+ ~wxListItemData();
void SetItem( const wxListItem &info );
void SetImage( int image ) { m_image = image; }
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
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 );
// 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 );
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();
// 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
// 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;
m_owner = owner;
- if ( owner->HasFlag(wxLC_REPORT) )
+ if ( owner->InReportView() )
{
m_rect = NULL;
}
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,
// 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();
wxListMainWindow::~wxListMainWindow()
{
- DeleteEverything();
+ DoDeleteAllItems();
delete m_highlightBrush;
}
ld->SetImage(listctrl->OnGetItemImage(line));
+ ld->SetAttr(listctrl->OnGetItemAttr(line));
}
wxListLineData *wxListMainWindow::GetDummyLine() const
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 );
CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
RefreshRect( rect );
-
}
else // !report
{
return;
}
+ if ( m_dirty )
+ {
+ // delay the repainting until we calculate all the items positions
+ return;
+ }
+
PrepareDC( dc );
int dev_x, dev_y;
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) );
}
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 );
}
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(),
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;
}
}
}
}
-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 );
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 );
}
}
m_selStore.SetItemCount(count);
m_countVirt = count;
- Refresh();
+ ResetVisibleLinesRange();
+
+ // scrollbars must be reset
+ m_dirty = TRUE;
}
int wxListMainWindow::GetSelectedItemCount()
// geometry calculation
// ----------------------------------------------------------------------------
-void wxListMainWindow::RecalculatePositions()
+void wxListMainWindow::RecalculatePositions(bool noRefresh)
{
- if ( IsEmpty() )
- return;
-
wxClientDC dc( this );
dc.SetFont( GetFont() );
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()
size_t index = (size_t)lindex;
- m_dirty = TRUE;
-
// select the next item when the selected one is deleted
- if ( m_current == index )
+ if ( m_current >= index )
{
- // the last valid index after deleting the item will be count-2
- if ( m_current == count - 1 )
- {
- m_current--;
- }
+ m_current--;
}
- SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM );
-
if ( InReportView() )
{
ResetVisibleLinesRange();
m_lines.RemoveAt( index );
}
+ m_dirty = TRUE;
+
+ SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM );
+
RefreshAfter(index);
}
m_columns.DeleteNode( node );
}
-void wxListMainWindow::DeleteAllItems()
+void wxListMainWindow::DoDeleteAllItems()
{
if ( IsEmpty() )
{
return;
}
- m_dirty = TRUE;
-
ResetCurrent();
// to make the deletion of all items faster, we don't send the
{
m_countVirt = 0;
- ResetVisibleLinesRange();
+ m_selStore.Clear();
}
if ( InReportView() )
}
m_lines.Clear();
+}
+
+void wxListMainWindow::DeleteAllItems()
+{
+ DoDeleteAllItems();
- m_selStore.Clear();
+ RecalculatePositions();
}
void wxListMainWindow::DeleteEverything()
_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) )
m_lines.Insert( line, id );
+ m_dirty = TRUE;
RefreshLines(id, GetItemCount() - 1);
}
{
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;
// 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
}
}
- wxASSERT_MSG( m_lineFrom <= m_lineTo && m_lineTo < GetItemCount(),
+ wxASSERT_MSG( IsEmpty() ||
+ (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
_T("GetVisibleLinesRange() returns incorrect result") );
if ( from )
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") );
m_mainWin->SetItemCount(count);
}
+void wxListCtrl::RefreshItem(long item)
+{
+ m_mainWin->RefreshLine(item);
+}
+
+void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
+{
+ m_mainWin->RefreshLines(itemFrom, itemTo);
+}
+
#endif // wxUSE_LISTCTRL