// The MSW version had problems with SetTextColour() et
// al as the wxListItemAttr's were stored keyed on the
// item index. If a item was inserted anywhere but the end
-// of the list the the text attributes (colour etc) for
+// of the list the text attributes (colour etc) for
// the following items were out of sync.
//
// Solution:
//
// However what we can do is store a pointer to a
// structure which contains the attributes we want *and*
-// a lParam for the users data, e.g.
+// a lParam -- and this is what wxMSWListItemData does.
//
-// class wxListItemInternalData
-// {
-// public:
-// wxListItemAttr *attr;
-// long lParam; // user data
-// };
-//
-// To conserve memory, a wxListItemInternalData is
+// To conserve memory, a wxMSWListItemData is
// only allocated for a LV_ITEM if text attributes or
// user data(lparam) are being set.
-
-
-// class wxListItemInternalData
-class wxListItemInternalData
+class wxMSWListItemData
{
public:
- wxListItemAttr *attr;
- LPARAM lParam; // user data
+ wxMSWListItemData() : attr(NULL), lParam(0) {}
+ ~wxMSWListItemData() { delete attr; }
- wxListItemInternalData() : attr(NULL), lParam(0) {}
- ~wxListItemInternalData()
- {
- if (attr)
- delete attr;
- }
+ wxListItemAttr *attr;
+ LPARAM lParam; // real user data
- wxDECLARE_NO_COPY_CLASS(wxListItemInternalData);
+ wxDECLARE_NO_COPY_CLASS(wxMSWListItemData);
};
// Get the internal data structure
-static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId);
-static wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId);
-static wxListItemAttr *wxGetInternalDataAttr(const wxListCtrl *ctl, long itemId);
-static void wxDeleteInternalData(wxListCtrl* ctl, long itemId);
+static wxMSWListItemData *wxGetInternalData(HWND hwnd, long itemId);
+static wxMSWListItemData *wxGetInternalData(const wxListCtrl *ctl, long itemId);
#if wxUSE_EXTENDED_RTTI
void wxListCtrl::Init()
{
- m_imageListNormal = NULL;
- m_imageListSmall = NULL;
+ m_imageListNormal =
+ m_imageListSmall =
m_imageListState = NULL;
- m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = false;
+ m_ownsImageListNormal =
+ m_ownsImageListSmall =
+ m_ownsImageListState = false;
+
m_colCount = 0;
m_count = 0;
- m_ignoreChangeMessages = false;
m_textCtrl = NULL;
- m_AnyInternalData = false;
+
m_hasAnyAttr = false;
}
void wxListCtrl::FreeAllInternalData()
{
- if (m_AnyInternalData)
- {
- int n = GetItemCount();
+ const unsigned count = m_internalData.size();
+ for ( unsigned n = 0; n < count; n++ )
+ delete m_internalData[n];
- m_ignoreChangeMessages = true;
- for (int i = 0; i < n; i++)
- wxDeleteInternalData(this, i);
- m_ignoreChangeMessages = false;
-
- m_AnyInternalData = false;
- }
+ m_internalData.clear();
}
void wxListCtrl::DeleteEditControl()
wxConvertToMSWListItem(this, info, item);
// we never update the lParam if it contains our pointer
- // to the wxListItemInternalData structure
+ // to the wxMSWListItemData structure
item.mask &= ~LVIF_PARAM;
// check if setting attributes or lParam
- if (info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA))
+ if ( info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA) )
{
// get internal item data
- // perhaps a cache here ?
- wxListItemInternalData *data = wxGetInternalData(this, id);
+ wxMSWListItemData *data = MSWGetItemData(id);
- if (! data)
+ if ( !data )
{
- // need to set it
- m_AnyInternalData = true;
- data = new wxListItemInternalData();
+ // need to allocate the internal data object
+ data = new wxMSWListItemData;
+ m_internalData.push_back(data);
item.lParam = (LPARAM) data;
item.mask |= LVIF_PARAM;
}
// user data
- if (info.m_mask & wxLIST_MASK_DATA)
+ if ( info.m_mask & wxLIST_MASK_DATA )
data->lParam = info.m_data;
// attributes
SetItem(info);
}
+// Gets the internal item data
+wxMSWListItemData *wxListCtrl::MSWGetItemData(long itemId) const
+{
+ LV_ITEM it;
+ it.mask = LVIF_PARAM;
+ it.iItem = itemId;
+
+ if ( !ListView_GetItem(GetHwnd(), &it) )
+ return NULL;
+
+ return (wxMSWListItemData *) it.lParam;
+}
+
// Gets the item data
wxUIntPtr wxListCtrl::GetItemData(long item) const
{
wxColour wxListCtrl::GetItemTextColour( long item ) const
{
wxColour col;
- wxListItemInternalData *data = wxGetInternalData(this, item);
+ wxMSWListItemData *data = MSWGetItemData(item);
if ( data && data->attr )
col = data->attr->GetTextColour();
wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
{
wxColour col;
- wxListItemInternalData *data = wxGetInternalData(this, item);
+ wxMSWListItemData *data = MSWGetItemData(item);
if ( data && data->attr )
col = data->attr->GetBackgroundColour();
wxFont wxListCtrl::GetItemFont( long item ) const
{
wxFont f;
- wxListItemInternalData *data = wxGetInternalData(this, item);
+ wxMSWListItemData *data = MSWGetItemData(item);
if ( data && data->attr )
f = data->attr->GetFont();
// inconsistent with the generic version - so we adjust the index
if (start != -1)
start --;
- return ListView_FindItem(GetHwnd(), (int) start, &findInfo);
+ return ListView_FindItem(GetHwnd(), start, &findInfo);
}
-// Find an item whose data matches this data, starting from the item after 'start'
-// or the beginning if 'start' is -1.
-// NOTE : Lindsay Mathieson - 14-July-2002
-// No longer use ListView_FindItem as the data attribute is now stored
-// in a wxListItemInternalData structure refernced by the actual lParam
+// Find an item whose data matches this data, starting from the item after
+// 'start' or the beginning if 'start' is -1.
long wxListCtrl::FindItem(long start, wxUIntPtr data)
{
- long idx = start + 1;
- long count = GetItemCount();
-
- while (idx < count)
+ // we can't use ListView_FindItem() directly as we don't store the data
+ // pointer itself in the control but rather our own internal data, so first
+ // we need to find the right value to search for (and there can be several
+ // of them)
+ int idx = wxNOT_FOUND;
+ const unsigned count = m_internalData.size();
+ for ( unsigned n = 0; n < count; n++ )
{
- if (GetItemData(idx) == data)
- return idx;
- idx++;
+ if ( m_internalData[n]->lParam == (LPARAM)data )
+ {
+ LV_FINDINFO findInfo;
+ findInfo.flags = LVFI_PARAM;
+ findInfo.lParam = (LPARAM)wxPtrToUInt(m_internalData[n]);
+
+ int rc = ListView_FindItem(GetHwnd(), start, &findInfo);
+ if ( rc != -1 )
+ {
+ if ( idx == wxNOT_FOUND || rc < idx )
+ {
+ idx = rc;
+ if ( idx == start + 1 )
+ {
+ // we can stop here, we don't risk finding a closer
+ // match
+ break;
+ }
+ }
+ //else: this item is after the previously found one
+ }
+ }
}
- return -1;
+ return idx;
}
// Find an item nearest this position in the specified direction, starting from
else if ( direction == wxLIST_FIND_RIGHT )
findInfo.vkDirection = VK_RIGHT;
- return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
+ return ListView_FindItem(GetHwnd(), start, &findInfo);
}
// Determines which item (if any) is at the specified point,
wxConvertToMSWListItem(this, info, item);
item.mask &= ~LVIF_PARAM;
- // check wether we need to allocate our internal data
- bool needInternalData = ((info.m_mask & wxLIST_MASK_DATA) || info.HasAttributes());
- if (needInternalData)
+ // check whether we need to allocate our internal data
+ bool needInternalData = (info.m_mask & wxLIST_MASK_DATA) ||
+ info.HasAttributes();
+ if ( needInternalData )
{
- m_AnyInternalData = true;
item.mask |= LVIF_PARAM;
- // internal stucture that manages data
- wxListItemInternalData *data = new wxListItemInternalData();
- item.lParam = (LPARAM) data;
+ wxMSWListItemData * const data = new wxMSWListItemData;
+ m_internalData.push_back(data);
+ item.lParam = (LPARAM)data;
- if (info.m_mask & wxLIST_MASK_DATA)
+ if ( info.m_mask & wxLIST_MASK_DATA )
data->lParam = info.m_data;
// check whether it has any custom attributes
struct wxInternalDataSort
{
wxListCtrlCompare user_fn;
- long data;
+ wxIntPtr data;
};
int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
- struct wxInternalDataSort *internalData = (struct wxInternalDataSort *) lParamSort;
+ wxInternalDataSort * const internalData = (wxInternalDataSort *) lParamSort;
- wxListItemInternalData *data1 = (wxListItemInternalData *) lParam1;
- wxListItemInternalData *data2 = (wxListItemInternalData *) lParam2;
+ wxMSWListItemData *data1 = (wxMSWListItemData *) lParam1;
+ wxMSWListItemData *data2 = (wxMSWListItemData *) lParam2;
long d1 = (data1 == NULL ? 0 : data1->lParam);
long d2 = (data2 == NULL ? 0 : data2->lParam);
}
-bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
+bool wxListCtrl::SortItems(wxListCtrlCompare fn, wxIntPtr data)
{
- struct wxInternalDataSort internalData;
+ wxInternalDataSort internalData;
internalData.user_fn = fn;
internalData.data = data;
const int iItem = nmLV->iItem;
- // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
- // ignored for efficiency. It is done here because the internal data is in the
- // process of being deleted so we don't want to try and access it below.
- if ( m_ignoreChangeMessages &&
- ( (nmLV->hdr.code == LVN_ITEMCHANGED) ||
- (nmLV->hdr.code == LVN_ITEMCHANGING)) )
- {
- return true;
- }
-
-
// If we have a valid item then check if there is a data value
// associated with it and put it in the event.
if ( iItem >= 0 && iItem < GetItemCount() )
{
- wxListItemInternalData *internaldata =
- wxGetInternalData(GetHwnd(), iItem);
+ wxMSWListItemData *internaldata =
+ MSWGetItemData(iItem);
if ( internaldata )
event.m_item.m_data = internaldata->lParam;
eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
event.m_itemIndex = iItem;
- // delete the assoicated internal data
- wxDeleteInternalData(this, iItem);
+
+ // delete the associated internal data
+ if ( wxMSWListItemData *data = MSWGetItemData(iItem) )
+ {
+ const unsigned count = m_internalData.size();
+ for ( unsigned n = 0; n < count; n++ )
+ {
+ if ( m_internalData[n] == data )
+ {
+ m_internalData.erase(m_internalData.begin() + n);
+ delete data;
+ data = NULL;
+ break;
+ }
+ }
+
+ wxASSERT_MSG( !data, "invalid internal data pointer?" );
+ }
break;
case LVN_INSERTITEM:
wxZeroMemory(lvhti);
#if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
- if(nmhdr->code == GN_CONTEXTMENU) {
- lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction;
- } else
+ if ( nmhdr->code == GN_CONTEXTMENU )
+ {
+ lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction;
+ }
+ else
#endif //__WXWINCE__
- ::GetCursorPos(&(lvhti.pt));
- ::ScreenToClient(GetHwnd(),&(lvhti.pt));
- if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 )
+ {
+ ::GetCursorPos(&(lvhti.pt));
+ }
+
+ ::ScreenToClient(GetHwnd(), &lvhti.pt);
+ if ( ListView_HitTest(GetHwnd(), &lvhti) != -1 )
{
if ( lvhti.flags & LVHT_ONITEM )
{
event.SetEventType(eventType);
+ // fill in the item before passing it to the event handler if we do have a
+ // valid item index and haven't filled it yet (e.g. for LVN_ITEMCHANGED)
+ if ( event.m_itemIndex != -1 && !event.m_item.GetMask() )
+ {
+ wxListItem& item = event.m_item;
+
+ item.SetId(event.m_itemIndex);
+ item.SetMask(wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE | wxLIST_MASK_DATA);
+ GetItem(item);
+ }
+
bool processed = HandleWindowEvent(event);
// post processing
wxListItemAttr *wxListCtrl::DoGetItemColumnAttr(long item, long column) const
{
- return IsVirtual() ? OnGetItemColumnAttr(item, column)
- : wxGetInternalDataAttr(this, item);
+ if ( IsVirtual() )
+ return OnGetItemColumnAttr(item, column);
+
+ wxMSWListItemData * const data = MSWGetItemData(item);
+ return data ? data->attr : NULL;
}
void wxListCtrl::SetItemCount(long count)
ListView_RedrawItems(GetHwnd(), itemFrom, itemTo);
}
-// ----------------------------------------------------------------------------
-// internal data stuff
-// ----------------------------------------------------------------------------
-
-static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId)
-{
- LV_ITEM it;
- it.mask = LVIF_PARAM;
- it.iItem = itemId;
-
- if ( !ListView_GetItem(hwnd, &it) )
- return NULL;
-
- return (wxListItemInternalData *) it.lParam;
-}
-
-static
-wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId)
-{
- return wxGetInternalData(GetHwndOf(ctl), itemId);
-}
-
-static
-wxListItemAttr *wxGetInternalDataAttr(const wxListCtrl *ctl, long itemId)
-{
- wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
-
- return data ? data->attr : NULL;
-}
-
-static void wxDeleteInternalData(wxListCtrl* ctl, long itemId)
-{
- wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
- if (data)
- {
- LV_ITEM item;
- memset(&item, 0, sizeof(item));
- item.iItem = itemId;
- item.mask = LVIF_PARAM;
- item.lParam = (LPARAM) 0;
- ListView_SetItem((HWND)ctl->GetHWND(), &item);
- delete data;
- }
-}
-
// ----------------------------------------------------------------------------
// wxWin <-> MSW items conversions
// ----------------------------------------------------------------------------
wxListItem& info,
LV_ITEM& lvItem)
{
- wxListItemInternalData *internaldata =
- (wxListItemInternalData *) lvItem.lParam;
+ wxMSWListItemData *internaldata =
+ (wxMSWListItemData *) lvItem.lParam;
if (internaldata)
info.m_data = internaldata->lParam;
const wxListItem& info,
LV_ITEM& lvItem)
{
+ if ( ctrl->InReportView() )
+ {
+ wxASSERT_MSG( 0 <= info.m_col && info.m_col < ctrl->GetColumnCount(),
+ "wxListCtrl column index out of bounds" );
+ }
+ else // not in report view
+ {
+ wxASSERT_MSG( info.m_col == 0, "columns only exist in report view" );
+ }
+
lvItem.iItem = (int) info.m_itemId;
lvItem.iImage = info.m_image;